I have a JSON file with some arrays in it. I want to iterate through the file arrays and get their elements and their values.
This is how my file looks like:
{ "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" } ] } }
This is how far I have come:
JSONObject object = new JSONObject("json-file.json"); JSONObject getObject = object.getJSONObject("JObjects"); JSONArray getArray = getObject.getJSONArray("JArray1"); for(int i = 0; i < getArray.length(); i++) { JSONObject objects = getArray.getJSONArray(i); //Iterate through the elements of the array i. //Get thier value. //Get the value for the first element and the value for the last element. }
Is it possible to do something like this?
The reason I want to do it like this is because the arrays in the file have a different number of elements.
How to iterate json array – JavaScript? To iterate JSON array, use the JSON.parse (). node fileName.js. Here, my file name is demo252.js.
You can loop over the Array like this: for(var i = 0; i < json.length; i++) { var obj = json[i];
6) We store each index record into a JSONObject. In order to get the JSON object of a particular index, we use getJSONObject () method of JSONArray. 7) To get a value of a specified field, we use the get () method of the JSONObject by passing the field name as a string in the get () method.
4) The getJSONArray () method takes array name as a string to retrieve from the Object. 5) We then use a for loop from 0 to JSONArray.length (). The length () method return the size of the array .
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 */
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With