Convert JSONarray to String Array

Convert JSONArray to String Array

Take a look at this tutorial.
Also you can parse above json like :

JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
list.add(arr.getJSONObject(i).getString("name"));
}

Convert JSONArray to String[] Array

i'm put the JSONArray to the string toString(), and i'm convert by regular expression like this ..

public String getImg(String d){
return rubahFormat(d).split(",");
}

public String rubahFormat(String d){
return d.replaceAll("[\\[\\]\\\"]","");
}

Thx ..

Convert string to JSON array

Here you get JSONObject so change this line:

JSONArray jsonArray = new JSONArray(readlocationFeed); 

with following:

JSONObject jsnobject = new JSONObject(readlocationFeed);

and after

JSONArray jsonArray = jsnobject.getJSONArray("locations");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
}

How to convert JSONArray to Java String Array

Use this function

String[] authors_arr(JSONArray authors){
String[] authors_res=new String[authors.length()];
try {
for(int i=0;i<authors.length();i++)
{
authors_res[i]=authors.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return authors_res;
}

call it like this String[] authors_string_array = authors_arr(authors);
The main point is to loop through your json array and add it into your array

convert json array into string array with only the value of json properties in react

  • To get the first one, you can use .reduce to return a list of keys representing the attributes found in the objects
  • To get the second one, you need to use .map twice to iterate over each item in the array, and each time, return values of the defined keys in this item

const data = [
{empnum:"10",fullname:"john doe",dateofhire:"2000-01-01:12:00:00", lastupdate:"2021-02-02"},
{empnum:"20",fullname:"john smith",dateofhire:"1990-02-01:12:00:00", lastupdate:"2021-01-02"},
{empnum:"30",fullname:"john wayne",dateofhire:"2005-01-03:12:00:00", lastupdate:"2021-02-01"},
]

// get properties from objects
const props = data.reduce((acc,item) =>
[...new Set([...acc, ...Object.keys(item)])]
, []);

// for each item, return array of values for the properties above
const records = data.map(item =>
props.map(key => item[key])
);

console.log(props);
console.log(records);

Java - Parse JSON Array of Strings into String Array

So I ended up getting it to work with these changes! I had never worked with JSON before this project and apparently I had it written wrong for what I was trying to do.

        JSONParser jsonParser2 = new JSONParser();
try (FileReader reader = new FileReader("Locations.json")) {
Object obj = jsonParser2.parse(reader);

JSONArray locationsList = (JSONArray) obj;

//Iterate over locations array
locationsList.forEach(emp -> parseJSONLocations((JSONObject) emp, locations, objects));

} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException | ParseException e) {
System.out.println(e);
}

and then the method to add it all to a class was changed as such:

    private static void parseJSONLocations(JSONObject locations, Locations[] location, Objects[] object) {
JSONObject locationObject = (JSONObject) locations.get("locations");
String desc = (String) locationObject.get("description");
String name = (String) locationObject.get("name");
JSONArray objects = (JSONArray) locationObject.get("objects");

Iterator<String> it = objects.iterator();
List<Objects> objs = new ArrayList<>();
while (it.hasNext()) {
String mStr = it.next();
for (Objects elm : object) {
if (elm.getName().equals(mStr)) {
objs.add(elm);
}
}
}

JSONArray directions = (JSONArray) locationObject.get("directions");
Map<String, String> map = new HashMap<>();
Iterator<JSONObject> it2 = directions.iterator();
while (it2.hasNext()) {
JSONObject value = it2.next();
map.put((String) value.get("direction"), (String) value.get("location"));
}
location[index2] = new Locations(desc, name, objs, map);
index2++;
}

But then I also had to change my JSON code, per jgolebiewski's suggestion:

[
{
"locations": {
"description": "You look around the room and see you are in an empty room with 2 doors to the left and to the right. Knowing not how you got there, you decide to figure out how to escape and get back to your normal life.",
"name": "start",
"objects": [],
"directions": [{
"direction": "right",
"location": "empty room1"
}, {
"direction": "left",
"location": "dungeon"
}]
}
},
{
"locations": {
"description": "Inside this room it looks like some sort of dungeon with a cage in the middle and blood lining the wall and floor.",
"name": "dungeon",
"objects": ["map", "torch"],
"directions": [{
"direction": "up",
"location": "hallway2"
}, {
"direction": "down",
"location": "hallway1"
}, {
"direction": "right",
"location": "start"
}]
}
}
]

Thank you for this tutorial for helping as well: https://howtodoinjava.com/library/json-simple-read-write-json-examples/

JSONArray to string array

Use gson. It's got a much friendlier API than org.json.

Collections Examples (from the User Guide):

Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);

//(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]

//(Deserialization)
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
//ints2 is same as ints

Convert Simple json Array to string Array In C#

If your string variable is the string representation of an array like

"['item1', 'item2', 'item3']"

Then you can deserialize it using one of the serializers. Here is one example using JSON.NET

var a = "['item1', 'item2', 'item3']";
string[] resultArray = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>(a);

The string [item1, item2, item3] does not look like the stringified version of an array and cannot be easily converted to an array like we did above, as it is.

If your string variable value is something like item1, item2, item3, you can call string.Split method which will give you an array.

var a = "item1, item2, item3";
string[] resultArray = a.Split(',');

Convert JSON array of objects to string array using Typescript

This should do the job:

names: string[] = jsonObject.map(person => person.name); 

// => ["Bill Gates", "Max Payne", "Trump", "Obama"]


Related Topics



Leave a reply



Submit