i am new to json and i have google a lot but can't find the solution.
this is my json file
{
"id": 1,
"firstname": "Katerina",
"languages": [
{
"lang": "en",
"knowledge": "proficient"
},
{
"lang": "fr",
"knowledge": "advanced"
}
]
}
i want to get the key and values of language arrays and print them like below:
key is: lang and value is: en
key is: knowledge and value is: proficient
key is: lang and value is: fr
key is: knowledge and value is: advanced
I used the below code to print:
JSONParser parser = new JSONParser();
File file = new File("/Users/amzadhossain/Documents/jsonfile2.json");
FileReader reader = new FileReader(file);
JSONObject jsonObject = (JSONObject) parser.parse(reader);
JSONArray languages = (JSONArray) jsonObject.get("languages");
Map<String,String> map = new HashMap<String,String>();
List<Map<String, String>> lstmap = new ArrayList<Map<String, String>>();
Iterator<String> iter = null;
Iterator<String> iter2 = null;
for (int i =0; i<languages.size();i++){
JSONObject firstarr = (JSONObject) languages.get(i);
iter = firstarr.keySet().iterator();
iter2 = firstarr.values().iterator();
while(iter.hasNext()){
String key = (String)iter.next();
//String value = firstarr.toJSONString();
String value = (String)iter2.next();
map.put(key,value);
lstmap.add(i, map);
System.out.println(i+" ***** "+key + "#### " + map.get(key));
}
}
for(int i=0;i<lstmap.size();i++){
Map<String,String> mapget = lstmap.get(i);
for (Map.Entry<String, String> entry : mapget.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}
}
But the problem is it is only printing the last key and values.please help.I want to solve this only by using json simple.