I just starting working with the java JSON library (org.json.simple.JSONObject).
In my code, I needed to make a JSONARRAY of JSONOBJECTS,
however instead of finding the 3 different objects in my array, I find the the last object duplicated.
The function code:
private void setMesurement(Date[] ts, Integer[][] time, String mesurementLabel, Object[][] mesurementValue, Integer[][] unit ) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.FRENCH);
JSONArray measurements = new JSONArray();
JSONObject series = new JSONObject();
JSONArray $_time = new JSONArray();
JSONArray msrVal = new JSONArray();
JSONArray unitVal = new JSONArray();
JSONObject msr = new JSONObject();
for (int i=0; i<ts.length; i++) {
$_time.clear();
msrVal.clear();
unitVal.clear();
for (int j=0; j<time[i].length; j++) {
$_time.add(time[i][j]);
msrVal.add(mesurementValue[i][j]);
unitVal.add(unit[i][j]);
}
series.put("$_time", $_time);
series.put(mesurementLabel, msrVal);
series.put("unit", unitVal);
msr.put("series", series);
msr.put("ts", dateFormat.format(ts[i]));
measurements.add(msr);
}
this.ppmp.put("measurements", measurements);
System.out.println("adding measurements: "+measurements+" to ppmp");
}
and this is the duplicated result:
"measurements":[
{"series":{
"heat":[7,8,9],
"unit":[2,2,2],
"$_time":[0,16,36]},
"ts":"2018-04-23T11:30:35.587+0100"},
{"series":{
"heat":[7,8,9],
"unit":[2,2,2],
"$_time":[0,16,36]},
"ts":"2018-04-23T11:30:35.587+0100"},
{"series":{
"heat":[7,8,9],
"unit":[2,2,2],
"$_time":[0,16,36]},
"ts":"2018-04-23T11:30:35.587+0100"}
]
You are not creating new JSON Objects for each iteration. You only create one at the beginning, set its values, and add it to the list. Then you overwrite the same object's values, and add it to the list again. In the end, you have added the object to the list three times, and its values will be the ones from the last iteration.
To fix this, change
JSONObject series = new JSONObject();
JSONArray $_time = new JSONArray();
JSONArray msrVal = new JSONArray();
JSONArray unitVal = new JSONArray();
JSONObject msr = new JSONObject();
for (int i=0; i<ts.length; i++) {
$_time.clear();
msrVal.clear();
unitVal.clear();
...
measurements.add(msr);
}
to
for (int i=0; i<ts.length; i++) {
JSONObject series = new JSONObject();
JSONArray $_time = new JSONArray();
JSONArray msrVal = new JSONArray();
JSONArray unitVal = new JSONArray();
JSONObject msr = new JSONObject();
...
measurements.add(msr);
}
This way, you create a new object for every step and avoid changing the previous ones.
Related
Here is my code for storing multiple JSON objects into an array and then I want to convert the array into a single string.
String message =null;
JSONObject json = new JSONObject();
json.put("name","student");
json.put("name", "Atal");
json.put("name","Rachit");
JSONArray array = new JSONArray();
for(int i= 0; i<= array.length(); i++) {
array.put(json);
message = array.toJSONString();
}
I want to get the output as a string and store it in a message variable.
String message =null;
JSONObject json = new JSONObject();
json.put("name","student");
json.put("name", "Atal");
json.put("name","Rachit");
JSONArray array = new JSONArray();
//for(int i= 0; i<= array.length(); i++) {
// array.put(json);
// message = array.toJSONString();
//}
array.put(json); //There is a JSON,just put it into the array
message = array.toJSONString();
I was replacing "name" every time I was calling json.put()
String message =null;
JSONObject json = new JSONObject();
json.put("name","Atal");
json.put("class", "10");
json.put("Roll","1035");
JSONArray array = new JSONArray();
array.put(json);
message = array.toJSONString();
How do I get a string from Parse JSON with data like below?
My Data JSON :
[
[
"John Gorman",
"3344764667200003",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Andy William",
"3403032311690003",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Thomas Endry",
"3408932311690078",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Robet Calm",
"3403077711690890",
"Student",
"2018-08-09 08:21:30.807"
]
]
I use the code below, then to get the string what I have to do, I really don't understand, if there is help, I really need to thank you.
try {
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("");
//JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString("What should I write here");
System.out.println(c.getString(""));
}
I want to get the string from Parse JSON that I have
How do I get a string for listview, for example:
John Gorman
3344764667200003
Student
Andy William
3403032311690003
Student
Thomas Endry
3408932311690078
Student
Robet Calm
3403077711690890
Student
A couple of things are wrong with the posted code. See my comments inline:
JSONObject object = new JSONObject(result);
^^^^^^^^^^^^^^^^^^^^^^
the content of result is not an object, but an array!
JSONArray jsonArray = object.getJSONArray("");
^^
a proper JSON document had better not have empty keys!
//JSONArray jsonArray = new JSONArray(result);
^^^^^^^^^^^^^^^^^^^^^
this was actually going in the right direction!
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
^^^^^^^^^^^^^^^^^^^^^^^^^^
the i-th element of the array is not an object, but an array!
Fixing the issues above:
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray subArray = jsonArray.getJSONArray(i);
for (int j = 0; j < subArray.length(); j++) {
System.out.println(subArray.getString(j));
}
System.out.println();
}
You are receiving a JSONArray not object so use the below code.
for(int i=0;i<jsonarray.length();i++){
JSONArray array=jsonarray.getJSONArray(i);
for(int j=0;j<array.length();j++){
Log.d(TAG,array.getString(j));
}
}
You can do something like this:
try {
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("");
//JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString(0);
String id = c.getString(1);
String student = c.getString(2);
// and so on ... This index refers to the index number on the string inside json object
System.out.println(name);
}
Hope this answers your query.
Following method will construct a list of string from the given Json. You can pass that list in the adapter of the list view.
public List<String> getList (String result) throws JSONException {
List<String> list = new LinkedList<>();
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray arr = jsonArray.getJSONArray(i);
StringBuilder sb = new StringBuilder();
for(int j=0;j<arr.length(); j++) {
sb.append(arr.getString(j));
if(j<arr.length()-1) sb.append("\n");
}
list.add(sb.toString());
}
return list;
}
I have a JSONArray :
public JSNArray getItems() {
JSONArray listjson = new JSONArray();
JSONObject fnl_json = new JSONObject();
//adding few items to fnl_json
listjson.add(fnl_json);
return fnal_json;
}
I am calling getItems function, how can I iterate through fnl_json from the called function?
To iterate a JSONArray you can do:
JSONObject jsonObject = new JSONObject("jsonString");
JSONArray jsonArray = jsonObject.getJSONArray("arrayName");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i); // you will get the json object
//do the stuff
}
For example, if I have a JSONObject that contains an array of 150 items, is there any way to make a JSONArray of only the first 5 items in the JSONObject?
I have this:
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = new JSONArray(jsonObject.getString("cast"));
But I want to avoid calling JSONArray jsonArray = new JSONArray(jsonObject.getString("cast")); because then I make an array of the entire JSONObject instead of just the first five items.
Thanks!
You can either modify the JSON response or create a new JSONArray and put the first five items. Please refer the code:
JSONArray studentArray = jsonObject.optJSONArray("students");
JSONArray firstFiveStudentArray = new JSONArray();
for (int i = 0; i <= 5; i++) {
JSONObject studentObj = studentArray.optJSONObject(i);
if (studentObj != null) {
firstFiveStudentArray.put(studentObj);
}
}
Lets say I have a JSON array like this
[{"a":"1", "b":"2"}, {"c":"3", "d":"4"}]
I'm trying to get it in Java like this
JSONArray array = new JSONArray(responseBody); //resposeBody is the JSON array
for (int i = 0; i < array.length(); i++) {
String arr = array.get(i).toString(); //Trying to get each array like this
JSONArray json = new JSONArray(arr);
}
At the line of JSONArray json = new JSONArray(arr);I get the error
A JSONArray text must start with '['
How do I access the values?
EDIT: I mean how do I get each array and their values
Your JSONArray is an array of JSONObjects.
You can try
JSONArray array = new JSONArray(responseBody);
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
}
Now you can further parse the individual JSONObjects.
Just do :
JSONObject myJsonObject = new JSONObject(jsonString);
In array:
JSONObject myjObject = myJsonArray.getJSONObject(i);