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
}
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 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.
I'm trying to save json_data from server to sqlite, but i'm getting the error
Value Next of type java.lang.String cannot be converted to JSONArray
Here is my json : My Json
And here is my java code :
String json_data = stringBuilder.toString().trim();
JSONObject object = new JSONObject(json_data);
JSONObject object1 = new JSONObject("Next");
JSONArray Jarray = object1.getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = Jarray.getJSONObject(i);
dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
}
dbHelper.close();
I know this code looks weird but please help me do this.
You need to fetch Next jsonobject instead of creating it here with a single String like
new JSONObject("Next"); , so fetch it from response object like
JSONObject object1 = object.getJSONObject("Next");
"Value Next of type java.lang.String cannot be converted to
JSONArray".
Rectify getJSONObject section.
Dont
JSONObject object1 = new JSONObject("Next");
Do
JSONObject obj = new JSONObject(json_data );
JSONObject _jsonOBJ = obj.getJSONObject("Next");
JSONArray Jarray = _jsonOBJ .getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = Jarray.getJSONObject(i);
dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
}
JSONObject result_obj= new JSONObject(result.toString());
JSONObject result_array= result_obj.getJSONObject("Next");
JSONArray array_values = result_array.getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject plan = Jarray.getJSONObject(i);
plan.getString("Day");
for(int j=0;j<plan.lenght();j++){
}
}
You need to Parse the "Next" object from the json provided. You have created a new JSONObject that was the mistake.
try {
String json_data = stringBuilder.toString().trim();
JSONObject object = new JSONObject(json_data);
JSONObject object1 =object.getJSONObject("Next");
JSONArray Jarray = object1.getJSONArray("Days");
for (int i = 0; i < Jarray.length(); i++)
{
JSONObject Jasonobject = Jarray.getJSONObject(i);
dbHelper.putStationsInformation((Jasonobject),sqLiteDatabase);
dbHelper.close();
}
} catch (JSONException e) {
e.printStackTrace();
}
Happy coding!!
You have created a new JSONObject("Next")which means you are converting a string "Next" into a JSONObject.
Rectify to this:
String json_data = stringBuilder.toString().trim();
JSONObject object = new JSONObject(json_data);
JSONObject object1 = object.getJSONObject("Next");
How can I convert this string into JSONArray ?
{"result":{"passion":[{"id":2,"description":"Sushi"},{"id":3,"description":"Dinner"},{"id":4,"description":"Sex"},{"id":5,"description":"Boobies"},{"id":6,"description":"Sleep"},{"id":7,"description":"Cars"},{"id":8,"description":"Travel"},{"id":9,"description":"Soccer"},{"id":10,"description":"Silice"}]}}
I'm trying to do:
JSONArray jsonArray = jsonObject.getJSONArray("passion");
But I am getting an exception:
org.json.JSONException: No value for passion
is seems like you are not getting result object
JSONArray jsonArray = jsonObject. getJSONObject("result").getJSONArray("passion");
may be this is what you need dear
ArrayList<String> stringArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray();
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
stringArray.add(jsonObject.toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
Use this code
JSONObject jsonObject = new JSONObject(json);
JSONObject resultValues = jsonObject.getJSONObject("result");
JSONArray passionArray = resultValues.getJSONArray("passion")