0

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.

2 Answers 2

1

check below code.

final JSONParser parser = new JSONParser();
final File file = new File("/Users/amzadhossain/Documents/jsonfile2.json");
final FileReader reader = new FileReader(file);
final JSONObject jsonObject = (JSONObject) parser.parse(reader);
final JSONArray languages = (JSONArray) jsonObject.get("languages");
List<Map<String, String>> lstmap = new ArrayList<Map<String, String>>();
        Iterator<String> iter = null;
        for (int i = 0; i < languages.size(); i++) {
            JSONObject firstarr = (JSONObject) languages.get(i);
            iter = firstarr.keySet().iterator();            
            while(iter.hasNext()){
                final Map<String, String> map = new HashMap<String, String>();
                String key = (String)iter.next();
                Object value = firstarr.get(key); 
                map.put(key, value.toString());
                lstmap.add(map);
               System.out.println("key is "+key + " and value is " + value);
            }

        }
        for (Map<String, String> map : lstmap){
            for (Map.Entry<String, String> entry : map.entrySet()) {
                System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

in outer for loop, its only print the last key and value ::: key is: lang and value is: fr key is: knowledge and value is: advanced but i want the first also.
0

This might help you,

  for(int i =0; i<languages.length();i++){

            lstmap = new ArrayList<Map<String, Object>>();
            JSONObject firstarr = (JSONObject) languages.get(i);
            iter = firstarr.keySet().iterator();            
            while(iter.hasNext()){
                String key = (String)iter.next();
                Object value = firstarr.get(key);             
               System.out.println("key is "+key + "and value is " + value);
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.