Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
var myJsonString = JSON.stringify(yourArray);
Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below
Answer from JonoW on Stack OverflowScript for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
var myJsonString = JSON.stringify(yourArray);
Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing. Credit goes to: @Spudley for his comment below
I made it that way:
if I have:
var jsonArg1 = new Object();
jsonArg1.name = 'calc this';
jsonArg1.value = 3.1415;
var jsonArg2 = new Object();
jsonArg2.name = 'calc this again';
jsonArg2.value = 2.73;
var pluginArrayArg = new Array();
pluginArrayArg.push(jsonArg1);
pluginArrayArg.push(jsonArg2);
to convert pluginArrayArg (which is pure javascript array) into JSON array:
var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))
java - Converting from JSONArray to String then back again - Stack Overflow
java - Convert string to JSON array - Stack Overflow
java - Convert JSONArray to String Array - Stack Overflow
Convert JSON string to array of JSON objects in Javascript - Stack Overflow
Is TableConvert really free to use?
Is my data secure when using this online converter?
What file size limits does the online converter have?
Videos
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}
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"));
}
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.
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