0

I have a Json file like this

{"Name":"Saaa","AppIcon":["ddd.jpg","Wallpaper.jpg","ddd.jpg"]}

I need to extract the AppIcon values. I'm using json simple lib to parse the json. The code snippet to parse the values is:

FileReader appIconReader = new FileReader("jsonpath.json");
JSONObject jsonIconObject = (JSONObject)jsonParser.parse(appIconReader);
System.out.println("APPLICATION ICON = "+jsonIconObject.get("AppIcon"));`

But the output what I'm getting is a single string:

["ddd.jpg","Wallpaper.jpg","ddd.jpg"]

I need to extract the individual values like this

ddd.jpg
Wallpaper.jpg
ddd.jpg

Not with the square brackets ([]) and double quotes ("") as I'm getting right now. How can I do that?

5
  • possible duplicate of How to parse the values? Commented Jul 9, 2014 at 12:05
  • 1
    You got answer here. stackoverflow.com/questions/24651923/how-to-parse-the-values/… Commented Jul 9, 2014 at 12:06
  • So you posted the same question on two different sites. Commented Jul 9, 2014 at 12:06
  • That's not valid JSON. Please update your question with the exact JSON you're dealing with. Commented Jul 9, 2014 at 12:07
  • @HotLicks: That was a typo...I edited my question. Commented Jul 9, 2014 at 12:12

2 Answers 2

3

JSON.simple isn't very thoroughly documented, but it looks like if you want to deal with an array, you need to cast the object returned by .get():

JSONArray appIcon = (JSONArray)jsonIconObject.get("AppIcon");

A JSONArray appears to be a generic container you can iterate, e.g.:

Iterator<String> it = appIcon.iterator();
while (it.hasNext()) System.out.println(it.next());
Sign up to request clarification or add additional context in comments.

Comments

0

you can try this one also and it is store value in list type :

 try {
        FileReader appIconReader = new FileReader("jsonpath.json");
        JSONObject jsonIconObject = (JSONObject)jsonParser.parse(appIconReader);
        JSONArray lan = jsonIconObject.get("AppIcon"));
        List<String> list = new ArrayList<String>();
        for(int i = 0; i < arr.length(); i++){
            list.add(lan.getJSONObject(i).getString("name"));
        }

    } catch (org.json.simple.parser.ParseException e) {
        e.printStackTrace();
    }

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.