开发者

How To parse this using GSON?

I've tried everything but i can't figure it out what Javabeans i need for this:

"poitypes":{
  "2":{
     "na开发者_运维技巧me":"Info",
     "icon":"info.png"
  },
  "1":{
     "name":"Toilets",
     "icon":"toilets.png"
  }
}

Does anyone have a clue?


You'd need something like this:

public class NamedIcon {
  private String name;
  private String icon;

  NamedIcon() {}

  public NamedIcon(String name, String icon) {
    this.name = name;
    this.icon = icon;
  }

  // Getters, equals, hashCode, toString, whatever else
}

And then some kind of wrapper class:

public class Wrapper {
  @SerializedName("1") private NamedIcon one;
  @SerializedName("2") private NamedIcon two;

  // ...
}

And then, since your "poitypes" field must itself be part of another JSON object (the snippet you've posted by itself isn't legal JSON), you'd need some class representing that, which would have a field called "poitypes" of type Wrapper (or whatever you call it).

Now, if your JSON object can actually contain more named icons (called "3", "4", etc.) then you wouldn't be able to do it quite like this. If those numbers are always going to be sequential, the JSON really ought to be a JSON array rather than an object. If they aren't, you'd need to use a custom JsonDeserializer to deserialize that object as a Map<Integer, NamedIcon> or some such.


if you are talking about the GSON library then you do something like this.

InputStreamReader isr = new InputStreamReader ( inputstream );
Gson g = new Gson ();
MyResultObject result = g.fromJson ( isr, MyResultObject.class );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜