2

I am trying to iterate through my json file and get required details here is my json

{
"000": {
    "component": "c",
    "determinantType": "dt",
    "determinant": "d",
    "header": "h",
    "determinantvalue": "null"
},
"001": {
    "component": "t",
    "determinantType": "i",
    "determinant":"ld",
    "header": "D",
    "determinantvalue": "null"
},
"002": {
    "component": "x",
    "determinantType": "id",
    "determinant": "pld",
    "header": "P",
    "determinantValue": "null"
}}

my java code

FileReader file = new FileReader("test.json");
Object obj = parser.parse(file);
System.out.println(obj);
JSONObject jsonObject = (JSONObject) obj;            
JSONArray msg = (JSONArray) jsonObject.get(key);          
Iterator<String> iterator = msg.iterator();         
while (iterator.hasNext()) {
System.out.println(iterator.next());            
String component = (String) jsonObject.get("component");           
System.out.println("component: " + component);           

As you can see in the code I am importing my json file and trying to get next elements and printing components out of it , I should also print header,determinant and determinant value as well Thank you

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
user3724559
  • 219
  • 1
  • 6
  • 20
  • 2
    Are you getting an error? What isn't working about this code? – Erik Dec 31 '14 at 11:02
  • You don't have any json arrays as far as I can see - and we don't know what `key` is. – Jon Skeet Dec 31 '14 at 11:03
  • JSONArray msg = (JSONArray) jsonObject.get(key); "Here i am trying to get all the keys i.e.,'000','001','002'" so that i can use it to iterate , but i am not able to get the keys – user3724559 Dec 31 '14 at 11:05
  • @user3724559 There is no JSONArray in your file. Only one object which have multiple objects with multiple properties. – Alexis C. Dec 31 '14 at 11:05
  • So are you saying I should not use JSONArray and I can not iterate and get all the elements ? – user3724559 Dec 31 '14 at 11:08
  • 1
    @user3724559: I'm saying you shouldn't use JSONArray because there aren't any arrays in your JSON. That's not the same as saying you can't iterate to get all the elements - see my answer. – Jon Skeet Dec 31 '14 at 11:09
  • If you consider your question answered, then you should accept an anwser. – stealthjong Jan 13 '15 at 08:56

3 Answers3

8

You don't have an array - you have properties with names of "000" etc. An array would look like this:

"array": [ {
    "foo": "bar1",
    "baz": "qux1"
  }, {
    "foo": "bar2",
    "baz": "qux2"
  }
]

Note the [ ... ] - that's what indicates a JSON array.

You can iterate through the properties of a JSONObject using keys():

// Unfortunately keys() just returns a raw Iterator...
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
    Object key = keys.next();
    JSONObject value = jsonObject.getJSONObject((String) key);
    String component = value.getString("component");
    System.out.println(component);
}

Or:

@SuppressWarnings("unchecked")
Iterator<String> keys = (Iterator<String>) jsonObject.keys();
while (keys.hasNext()) {
    String key = keys.next();
    JSONObject value = jsonObject.getJSONObject(key);
    String component = value.getString("component");
    System.out.println(component);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you , i will make my json file an json array and try iterating – user3724559 Dec 31 '14 at 11:24
  • @user3724559: Well if you *can* change the format of the file, that would be ideal. But if not, my answer should show how you can iterate anyway... – Jon Skeet Dec 31 '14 at 11:35
  • I used keyset and iterated but it is unordered , is there anyway I can sort it ? As you can see my JSON i have given it as 000,001,002 so that i can order my json – user3724559 Dec 31 '14 at 11:56
  • @user3724559: Well you could create a `List` from the keys and then call `Collections.sort` before fetching them... – Jon Skeet Dec 31 '14 at 11:57
  • We can not convert set to list – user3724559 Dec 31 '14 at 12:01
  • @user3724559: I'm not sure what set you're talking about - there's no set in my answer. But you can easily create an `ArrayList` and populate it from an iterator... – Jon Skeet Dec 31 '14 at 12:02
0

try this...

FileReader file = new FileReader("test.json");
Object obj = parser.parse(file);
System.out.println(obj);
JSONObject jsonObject = (JSONObject) obj;            

Iterator<String> iterator = jsonObject .iterator();  

for(Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext();) {
    String key = (String) iterator.next();
    System.out.println(jsonObject.get(key));
}
Dhaval
  • 2,341
  • 1
  • 13
  • 16
0

There's not a JSONArray, only a few JSONObjects. Iterate the keys of the main JSONObject with JSONObject.keys().

public static final String COMPONENT = "component";
public static final String DT = "determinantType";
public static final String D = "determinant": "d";
public static final String HEADER = "header";
public static final String DV = "determinantvalue";

JSONObject jso = getItFromSomewhere();
for (Object key : jso.keys()) {
    JSONObject subset = jso.getJSONObject(key);
    String d = subset.getString(D);
    String header = subset.getString(HEADER);
    String dv = subset.getString(DV);
    System.out.println(key + " " + header + " " + d + " " + dv);
}
stealthjong
  • 10,858
  • 13
  • 45
  • 84