Change
JSONObject objects = getArray.getJSONArray(i);
to
JSONObject objects = getArray.getJSONObject(i);
or to
JSONObject objects = getArray.optJSONObject(i);
depending on which JSON-to/from-Java library you're using. (It looks like getJSONObject will work for you.)
Then, to access the string elements in the "objects" JSONObject, get them out by element name.
String a = objects.get("A");
If you need the names of the elements in the JSONObject, you can use the static utility method JSONObject.getNames(JSONObject) to do so.
String[] elementNames = JSONObject.getNames(objects);
"Get the value for the first element and the value for the last element."
If "element" is referring to the component in the array, note that the first component is at index 0, and the last component is at index getArray.length() - 1.
I want to iterate though the objects in the array and get thier component and thier value. In my example the first object has 3 components, the scond has 5 and the third has 4 components. I want iterate though each of them and get thier component name and value.
The following code does exactly that.
import org.json.JSONArray;
import org.json.JSONObject;
public class Foo
{
public static void main(String[] args) throws Exception
{
String jsonInput = "{\"JObjects\":{\"JArray1\":[{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\"},{\"A\":\"a1\",\"B\":\"b2\",\"C\":\"c3\",\"D\":\"d4\",\"E\":\"e5\"},{\"A\":\"aa\",\"B\":\"bb\",\"C\":\"cc\",\"D\":\"dd\"}]}}";
// "I want to iterate though the objects in the array..."
JSONObject outerObject = new JSONObject(jsonInput);
JSONObject innerObject = outerObject.getJSONObject("JObjects");
JSONArray jsonArray = innerObject.getJSONArray("JArray1");
for (int i = 0, size = jsonArray.length(); i < size; i++)
{
JSONObject objectInArray = jsonArray.getJSONObject(i);
// "...and get thier component and thier value."
String[] elementNames = JSONObject.getNames(objectInArray);
System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
for (String elementName : elementNames)
{
String value = objectInArray.getString(elementName);
System.out.printf("name=%s, value=%s\n", elementName, value);
}
System.out.println();
}
}
}
/*
OUTPUT:
3 ELEMENTS IN CURRENT OBJECT:
name=A, value=a
name=B, value=b
name=C, value=c
5 ELEMENTS IN CURRENT OBJECT:
name=D, value=d4
name=E, value=e5
name=A, value=a1
name=B, value=b2
name=C, value=c3
4 ELEMENTS IN CURRENT OBJECT:
name=D, value=dd
name=A, value=aa
name=B, value=bb
name=C, value=cc
*/
Answer from Programmer Bruce on Stack OverflowChange
JSONObject objects = getArray.getJSONArray(i);
to
JSONObject objects = getArray.getJSONObject(i);
or to
JSONObject objects = getArray.optJSONObject(i);
depending on which JSON-to/from-Java library you're using. (It looks like getJSONObject will work for you.)
Then, to access the string elements in the "objects" JSONObject, get them out by element name.
String a = objects.get("A");
If you need the names of the elements in the JSONObject, you can use the static utility method JSONObject.getNames(JSONObject) to do so.
String[] elementNames = JSONObject.getNames(objects);
"Get the value for the first element and the value for the last element."
If "element" is referring to the component in the array, note that the first component is at index 0, and the last component is at index getArray.length() - 1.
I want to iterate though the objects in the array and get thier component and thier value. In my example the first object has 3 components, the scond has 5 and the third has 4 components. I want iterate though each of them and get thier component name and value.
The following code does exactly that.
import org.json.JSONArray;
import org.json.JSONObject;
public class Foo
{
public static void main(String[] args) throws Exception
{
String jsonInput = "{\"JObjects\":{\"JArray1\":[{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\"},{\"A\":\"a1\",\"B\":\"b2\",\"C\":\"c3\",\"D\":\"d4\",\"E\":\"e5\"},{\"A\":\"aa\",\"B\":\"bb\",\"C\":\"cc\",\"D\":\"dd\"}]}}";
// "I want to iterate though the objects in the array..."
JSONObject outerObject = new JSONObject(jsonInput);
JSONObject innerObject = outerObject.getJSONObject("JObjects");
JSONArray jsonArray = innerObject.getJSONArray("JArray1");
for (int i = 0, size = jsonArray.length(); i < size; i++)
{
JSONObject objectInArray = jsonArray.getJSONObject(i);
// "...and get thier component and thier value."
String[] elementNames = JSONObject.getNames(objectInArray);
System.out.printf("%d ELEMENTS IN CURRENT OBJECT:\n", elementNames.length);
for (String elementName : elementNames)
{
String value = objectInArray.getString(elementName);
System.out.printf("name=%s, value=%s\n", elementName, value);
}
System.out.println();
}
}
}
/*
OUTPUT:
3 ELEMENTS IN CURRENT OBJECT:
name=A, value=a
name=B, value=b
name=C, value=c
5 ELEMENTS IN CURRENT OBJECT:
name=D, value=d4
name=E, value=e5
name=A, value=a1
name=B, value=b2
name=C, value=c3
4 ELEMENTS IN CURRENT OBJECT:
name=D, value=dd
name=A, value=aa
name=B, value=bb
name=C, value=cc
*/
for (int i = 0; i < getArray.length(); i++) {
JSONObject objects = getArray.getJSONObject(i);
Iterator key = objects.keys();
while (key.hasNext()) {
String k = key.next().toString();
System.out.println("Key : " + k + ", value : "
+ objects.getString(k));
}
// System.out.println(objects.toString());
System.out.println("-----------");
}
Hope this helps someone
Not with an iterator.
For org.json.JSONArray, you can do:
Copyfor (int i = 0; i < arr.length(); i++) {
arr.getJSONObject(i);
}
For javax.json.JsonArray, you can do:
Copyfor (int i = 0; i < arr.size(); i++) {
arr.getJsonObject(i);
}
You can use the opt(int) method and use a classical for loop.
Seems like you can't iterate through JSONArray with a for each. You can loop through your JSONArray like this:
for (int i=0; i < arr.length(); i++) {
arr.getJSONObject(i);
}
Source
Apparently, org.json.simple.JSONArray implements a raw Iterator. This means that each element is considered to be an Object. You can try to cast:
for(Object o: arr){
if ( o instanceof JSONObject ) {
parse((JSONObject)o);
}
}
This is how things were done back in Java 1.4 and earlier.
You can use the following code:
//put your json in the string variable "data"
JSONArray jsonArray=new JSONArray(data);
if(jsonArray!=null && jsonArray.length()>0){
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray childJsonArray=jsonArray.optJSONArray(i);
if(childJsonArray!=null && childJsonArray.length()>0){
for (int j = 0; j < childJsonArray.length(); j++) {
System.out.println(childJsonArray.optString(j));
}
}
}
}
It looks like the JSON array is 2 dimensional. Try this:
JSONArray outerArray = new JSONArray(data);
for (int i = 0; i < outerArray.length(); i++) {
JSONArray innerArray = outerArray.getJSONArray(i);
for (int j = 0; j < outerArray.length(); j++) {
System.out.println(innerArray.get(j));
}
}
In your code the element dataArray is an array of JSON objects, not a JSON object itself. The elements A, B, and C are part of the JSON objects inside the dataArray JSON array.
You need to iterate over the array
public static void main(String[] args) throws Exception {
String jsonStr = "{ \"dataArray\": [{ \"A\": \"a\", \"B\": \"b\", \"C\": \"c\" }, { \"A\": \"a1\", \"B\": \"b2\", \"C\": \"c3\" }] }";
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray c = jsonObj.getJSONArray("dataArray");
for (int i = 0 ; i < c.length(); i++) {
JSONObject obj = c.getJSONObject(i);
String A = obj.getString("A");
String B = obj.getString("B");
String C = obj.getString("C");
System.out.println(A + " " + B + " " + C);
}
}
prints
a b c
a1 b2 c3
I don't know where msg is coming from in your code snippet.
Java Docs to the rescue:
You can use http://www.json.org/javadoc/org/json/JSONObject.html#getJSONArray(java.lang.String) instead
JSONArray dataArray= sync_reponse.getJSONArray("dataArray");
for(int n = 0; n < dataArray.length(); n++)
{
JSONObject object = dataArray.getJSONObject(n);
// do some stuff....
}
Your code is absolutely correct, it works fine with org.json.simple:
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonTest {
public static void main(String[] args) throws ParseException {
JSONArray c = (JSONArray) new JSONParser()
.parse("[ { \"name\":\"test\", \"age\":1 }, "
+ "{ \"name\":\"test\", \"age\":1 } ]");
for (int i = 0; i < c.size(); i++) {
JSONObject obj = (JSONObject) c.get(i);
System.out.println(obj.get("name"));
}
}
}
It outputs:
test
test
Check how input JSONArray was created. It's possible that there's something different inside it. For example, it's possible that you have non-printable character in key name, so you don't see it when using c.toString(), but obj.get("name") fails.
You can iterate over the JSONArray elements using an Iterator, like this:
//arr is your JSONArray here
Iterator<Object> iterator = arr.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if(obj instanceof JSONObject) {
System.out.println(obj.get("name"));
}
}
It uses org.json.simple.JSONObject and org.json.simple.JSONArray.
You want something like this..
JSONArray jsonArray = new JSONArray(sJSON);
JSONArray jsonPersonData = jsonArray.getJSONArray(1);
for (int i=0; i<jsonPersonData.length(); i++) {
JSONObject item = jsonPersonData.getJSONObject(i);
String name = item.getString("name");
String surname = item.getString("surname");
}
You shoul not use JSONObject.toString. This is how you should iterate your array:
for(int i=0;i<arrJSON.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String name =json_data.getString("name");
String surname =json_data.getString("surname");
}