【问题标题】:How to parse nested JSON from android? [duplicate]如何从android解析嵌套的JSON? [复制]
【发布时间】:2016-10-22 12:06:33
【问题描述】:

我有下面的示例 json 数据。如何解析?

"posts": [12]
0:  {
"id": 6429
"type": "post"
"slug": "pyo-puth-remix-lyrics-dj-frenzy"
"url": "http://song.com/2016/06/remix-lyrics.html"
"status": "publish"
"title": " Remix Lyrics – DJ Frenzy"
"title_plain": " Remix Lyrics "
"content": "<p><strong>Remix lyrics</strong> is latest  single track produced by <strong><a >"

【问题讨论】:

    标签: android


    【解决方案1】:

    不要在 cmets 中听这些家伙的意见。您不必重新发明轮子并实现自己的 Json 解析器。您可以使用任何库,例如 Moshi:

    创建 Json 表示:

    public class Dummy {
        int value1;
        List<InnerDummy> valueList;
    }
    
    public class InnerDummy {
        int value1;
        boolean value2;
    }
    

    使用它们来解析您的 Json:

    String json = "...";
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<Dummy> jsonAdapter = moshi.adapter(Dummy.class);
    Dummy dummy = jsonAdapter.fromJson(json);
    

    【讨论】:

    • 这是正确的做法
    猜你喜欢
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多