如何从Android解析嵌套的JSON?
如何从Android解析嵌套的JSON?
我有下面的示例 JSON 数据。我该如何解析它?\n
"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": "Remix lyrics is latest single track produced by "
问题出现的原因:有人在评论中建议自己重新发明轮子,实现自己的Json解析器,但事实上不需要这样做。可以使用现有的库来解析Json。
解决方法:可以使用任何库来解析Json,例如Moshi库。首先创建Json表示,然后使用它们来解析Json。
以下是具体的解决方法:
1. 创建Json表示:
public class Dummy { int value1; ListvalueList; } public class InnerDummy { int value1; boolean value2; }
2. 使用它们来解析Json:
String json = "..."; Moshi moshi = new Moshi.Builder().build(); JsonAdapterjsonAdapter = moshi.adapter(Dummy.class); Dummy dummy = jsonAdapter.fromJson(json);
这是正确的方法。