Hello.
I am trying to read a .json files content which work but then I need to use the json data somehow. Ive been trying to fix so i can use the json object keys but I cant get it to work. Mayebe someone can help me?
the json looks like this
[CODE]
{
“2”:{
“id”:2,
“name”: “test”
},
“6”:{
“id”:6,
“name”: “test”
},
“8”:{
“id”:8,
“name”: “test”
}
}
PS: Java is VERY new for me
This is what ive done so far
[CODE][
//throws Exception
String key = “”;
FileReader file = new FileReader(“file.json”);
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
while (line != null) {
key += line;
line = reader.readLine();
}
StringReader asd = new StringReader(key);
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(asd);
//System.out.println(json);
for (Object key1 : json.keySet()) {
String keyStr = (String)key1;
Object keyvalue = json.get(keyStr);
// this does not work at all – I want only the item which has keyStr “2”
if(keyStr == “2”) {
System.out.println(“key: “+ keyStr + ” value: ” + keyvalue);
}
}
}
/CODE]