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"));
}
Answer from Saad Asad on Stack OverflowTake 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"));
}
Simplest and correct code is:
public static String[] toStringArray(JSONArray array) {
if(array==null)
return new String[0];
String[] arr=new String[array.length()];
for(int i=0; i<arr.length; i++) {
arr[i]=array.optString(i);
}
return arr;
}
Using List<String> is not a good idea, as you know the length of the array.
Observe that it uses arr.length in for condition to avoid calling a method, i.e. array.length(), on each loop.
java - JSONArray to string array - Stack Overflow
java - Converting from JSONArray to String then back again - Stack Overflow
android - JSONArray to String in java - Stack Overflow
jquery - Convert JSONArray to String Array with custom format - Stack Overflow
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
You are right! Even @Sean Patrick Floyd's answer is too much boilterplate to covert a JSON array to string[] or any other type of array class. Rather here is what I find to be elegant:
JsonArray jsonArray = input.getAsJsonArray("key");
Gson gson = new Gson();
String[] output = gson.fromJson(jsonArray , String[].class)
NOTE 1: JsonArray must be an array of strings, for the above example, without any property names. Eg:
{key:["Apple","Orange","Mango","Papaya","Guava"]}
Note 2: JsonObject class used above is from com.google.gson library and not the JSONObject class from org.json library.
The problem is not the JSONArray.toString(), as @Selvin mentioned.
From JSONArray source:
/**
* Encodes this array as a compact JSON string, such as:
* <pre>[94043,90210]</pre>
*/
@Override public String toString() {
try {
JSONStringer stringer = new JSONStringer();
writeTo(stringer);
return stringer.toString();
} catch (JSONException e) {
return null;
}
}
/**
* Encodes this array as a human readable JSON string for debugging, such
* as:
* <pre>
* [
* 94043,
* 90210
* ]</pre>
*
* @param indentSpaces the number of spaces to indent for each level of
* nesting.
*/
public String toString(int indentSpaces) throws JSONException {
JSONStringer stringer = new JSONStringer(indentSpaces);
writeTo(stringer);
return stringer.toString();
}
The problem is that you need to convert your ChecklistAnswer to JSON object first for your JSONArray to work properly.
Again from Javadoc:
/**
* A dense indexed sequence of values. Values may be any mix of
* {@link JSONObject JSONObjects}, other {@link JSONArray JSONArrays}, Strings,
* Booleans, Integers, Longs, Doubles, {@code null} or {@link JSONObject#NULL}.
* Values may not be {@link Double#isNaN() NaNs}, {@link Double#isInfinite()
* infinities}, or of any type not listed here.
...
In my ChecklistAnwer class i added:
public JSONObject toJsonObject(){
JSONObject json = new JSONObject();
try {
json.put("questionId", questionId);
json.put("checklistId", checklistId);
json.put("answer", answer);
json.put("remark", remark);
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
and in my other class:
JSONArray answers = new JSONArray();
ChecklistAnswer answer = new ChecklistAnswer(questions.get(id).id, 2, cb.isChecked(), text.getText().toString());
answers.put(answer.toJsonObject());
if i filled the array:
String js = answers.toString(1);
and that returns:
[
{
"answer": true,
"questionId": 1,
"remark": "",
"checklistId": 2
},
{
"answer": false,
"questionId": 4,
"remark": "teesxfgtfghyfj",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
},
{
"answer": true,
"questionId": 4,
"remark": "",
"checklistId": 2
}
]
thanks to @Selvin
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);
}
Input String
[
{
"userName": "sandeep",
"age": 30
},
{
"userName": "vivan",
"age": 5
}
]
Simple Way to Convert String to JSON
public class Test
{
public static void main(String[] args) throws JSONException
{
String data = "[{\"userName\": \"sandeep\",\"age\":30},{\"userName\": \"vivan\",\"age\":5}] ";
JSONArray jsonArr = new JSONArray(data);
for (int i = 0; i < jsonArr.length(); i++)
{
JSONObject jsonObj = jsonArr.getJSONObject(i);
System.out.println(jsonObj);
}
}
}
Output
{"userName":"sandeep","age":30}
{"userName":"vivan","age":5}
So I've been trying to tackle this for a while and am a little stuck.
-
The flow reads output from Excel into an Array (using Filter Array)
-
I use String() to convert the Array to a single, very long string (Still in the jsonFormat)
So from
[{
"@odata.etag": "",
"Item": "12345",
"figures": "12",
"sourcename[Customername]": "Timmy"
},
{
"@odata.etag": "",
"Item": "567",
"figures": "9",
"sourcename[Customername]": "James"
}
]This then becomes flat and is 1 very long string. (I can't use SELECT as Key names randomly have a prefix to them.
{"@odata.etag": "","Item": "12345","figures": "12","sourcename[Customername]": "Timmy"},{"@odata.etag": "","Item": "567","figures": "9","sourcename[Customername]": "James"}3. I then use Compose > and the the replace() function to Analyse the long string and remove "Sourcename[" and ]
So far so good!
1 very long Json String and it's now in the right format (Customername)
--
This is where I run into trouble.
When I use Json() function to convert it back , I only get the very first Object (Where the customer name is Timmy.
anytime I use
json(outputs('compose_3')) 'Compose_3 being the very long Json string (string format).I get this issue.
If I try splitting the string, I end up which a whole heap of "/" in the String (Which is what you expect when you convert a string to Array, you get a badly formatted Array.
"{\"@odata.etag\"\: \"\",\ etc These are escape characters in the backend (That compose normally filters out visually), but using the Split, it brings them to the front of view.
So I'm stuck on the correct way to handle this very long string and getting it to appear as multiple entries in ParseJson etc. I tried adding a manual "Apply To Each" but kept hitting the same issue with JSON() only returns Timmy and not any other records.
Any thoughts on what I can try here? This is the last step to get this working correctly.