0

This what my Json file looks like.

[
   [
    "Name"
    "Age"
    "Height"
   ]
   [
    "Name"
    "Age"
    "Height"
   ]
]

How can I read the specific value like "Name" using simple.json in Java?

Alpha08
  • 5
  • 3

1 Answers1

1

This is invalid json format. But anyway to read json data from a file you can open it and read it to string as shown in here

Then using org.json library you can parse string to either json array or object.

String fileDataJsonObject = "{"Name":"SomeName"}"; //string containing a json object
String fileDataJsonArray = "[{"Name":"SomeName"},{"Name":"SomeName2"}]"; //string containing a json array
 
JSONObject obj = new JSONObject(fileDataJsonObject);
JSONArray array = new JSONArray(fileDataJsonArray);

This is just an example of how you can parse string to json array or an object. For arrays you can then use getJSONObject(index) method to get json objects from array. Also you can use json object getters to get any element within that object.

p.s I just found this useful discussion on stackoverflow. The answers have different ways of parsing json object or array using different libraries.