Java - retrieving JSON object values - java

i have a JSON object which looks something like this:
cb = {"content":[{"name":"abc"}{"name":"bcd"}{"name":"xyz"}...]}
and i have imported the JSON library as:
import org.json.JSONObject;
Now i want to use a for loop to retrieve the values of name in each of the sub objects...
for(int x = 10; x < numberOfSubElemtnts; x = x+1) {
//print out the name values
}
Output should be:
abc
bcd
xyz
How should I evaluate the length of the array and print out the name values?

You can iterate the array like this:
String content = "{\"content\":[{\"name\":\"abc\"},{\"name\":\"bcd\"},{\"name\":\"xyz\"}]}";
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("content");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
System.out.println(object.getString("name"));
}

JSONParser parser = new JSONParser();
String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
try{
Object obj = parser.parse(s);
JSONArray array = (JSONArray)obj;
for(int i=0;i<array.length();i++{
System.out.println(array.get(i).getString("name");
}
}catch(ParseException pe){
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
Also with this example youll have to change the format of your string to fit the one I have in mine

Related

want to store multiple JSON objects to jsonArray and get the result as a single string

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();

single array json parser in java

I have a json string which i need to parse using java. Format is -
input = {"data":{"value":[654,123]},"address":null,"code":null}
From this i need only "value":[654,123]
I tried but this -
JSONParser parse = new JSONParser();
JSONObject jobj = (JSONObject)parse.parse(input);
JSONArray jsonarr_1 = (JSONArray) jobj.get("value");
System.out.println(jsonarr_1);
for(int i=0;i<jsonarr_1.size();i++)
{
JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);
JSONArray jsonarr_2 = (JSONArray) jsonobj_1.get("value");
System.out.println("values under value array");
System.out.println(jsonarr_2);
}
}
is first level of json array and giving nullpointerexception
You first need to get data Json object and then read the value array from that.
You can see working code below:
String input = "{\"data\":{\"value\":[654,123]},\"address\":null,\"code\":null}";
JSONParser parse = new JSONParser();
JSONObject jobj = (JSONObject) parse.parse(input);
JSONObject dataObj = (JSONObject) jobj.get("data");
JSONArray jsonarr_1 = (JSONArray) dataObj.get("value");
System.out.println(jsonarr_1);
for (int i = 0; i < jsonarr_1.size(); i++) {
System.out.println("values under value array");
long value = (long) jsonarr_1.get(i);
System.out.println(value);
}

Android - Parse PHP json_encode to Java

String json_string_from_server = "{\"test1\":\"test1_value\",\"test2\":\"test2_value\"}";
how to loop the JSON and print all the keys and values.
You can simply parse the Json string as below -
String json_string_from_server = "{\"test1\":\"test1_value\",\"test2\":\"test2_value\"}";
JSONObject jObj = new JSONObject(json_string_from_server);
String val_Test1 = jObj.getString("test1");
String val_Test2 = jObj.getString("test2");
case 2:
String json_string_from_server = "{
"result" : [
{\"test1\":\"test1_values_baru\", \"test2\":\"test2_values\"},
{\"test1\":\"test‌​1_values\", \"test2\":\"test2_values\"}
]
}";
JSONObject jObj = new JSONObject(json_string_from_server);
JSONArray jResultArray = jObj.getJSONArray("result");
for(int i=0; i<jResultArray.length(); i++){
JSONObject jResultObj = jResultArray.getJSONObject(i);
String val_Test1 = jResultObj.getString("test1");
String val_Test2 = jResultObj.getString("test2");
}
Suppose jsonString is a variable in which you get all your json from php. You will need to iterate the array as follows:
JSONArray arr = new JSONArray(JsonString);
for(int i=0; i<= arr.length(); i++)
{
JSONObject obj = arr.get(i);
obj.getString('test1'); // these are your strings
obj.getString('test2');
}
String json_string="[{\"test1\":\"test1_values_baru\",\"test2\":\"test2_values\"},{\"test1\":\"test‌​1_values\",\"test2\":\"test2_values\"}]";
JSONArray jResultArray = new JSONArray(json_string);
for(int i=0; i<jResultArray.length(); i++){
JSONObject jResultObj = jResultArray.getJSONObject(i);
String val_Test1 = jResultObj.getString("test1");
String val_Test2 = jResultObj.getString("test2");
}

How to access key:values in JSON using Java with no array names

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);

Get JSON String in java with org.json

[{"post":{"titel":"Glee","interpret":"Bran Van 3000","jahr":"1997","id":"5"}},{"post":{"titel":"Goodbye Country (Hello Nightclub)","interpret":"Groove Armada","jahr":"2001","id":"4"}},{"post":{"titel":"Beauty","interpret":"Ryuichi Sakamoto","jahr":"1990","id":"1"}}]
The above string is what i have in json and now what i want to do is to just get the values of "post" and "titel" as in the above string what i am having 3 values of post which makes sense to me. But, now what i want is to print all the 3 values of post and each value in the post string.
JSONArray jsonArray = new JSONArray(jsonString);
columns = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
columns[i]=jsonObject.getString("post");
}
above code returns me 3 values of post. WHat i can do if i want to print values of post and the each value of column in post?
I would do something like this (I don't use the columns array just to print, and this code is not tested):
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject postObj = jsonObject.getJSONObject("post");
for (String i : postObj.keys()) {
System.out.println("Key: " + i + "; Value: " + postObj.getString(i));
}
}
That is, process each element of the Object returned by the "post" object of each array element.

Categories