Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having 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 as below.

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 gettin is a single string as below: ["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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
145 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try with JSON.

    String str="["ddd.jpg","Wallpaper.jpg","ddd.jpg"]";
    Type collectionType = new TypeToken<String[]>() {
    }.getType();
    String[] a=new Gson().fromJson(str,collectionType);
    for (String i:a){
        System.out.println(i);
    }

Output

   ddd.jpg
   Wallpaper.jpg
   ddd.jpg

Edit: for your edited question answer like this.

 public class Obj{
  private String name;
  private List<String> appIcons;

  public List<String> getAppIcons() {
     return appIcons;
  }

  public void setAppIcons(List<String> appIcons) {
     this.appIcons = appIcons;
  }

  public String getName() {
     return name;
  }

  public void setName(String name) {
     this.name = name;
  }
}

Now you can simply pass your JSON

String str = "{"name":"Saaa","appIcons":
                               ["ddd.jpg","Wallpaper.jpg","ddd.jpg"]}";
Obj obj = new Gson().fromJson(str, Obj.class);
System.out.println(obj.getAppIcons());

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...