Android Studio 解析 JSON 对象不起作用

huangapple go评论81阅读模式
英文:

Android Studio Parsing JSON object doesn't work

问题

这是我的 JSON 文件。我试图将 JSON 加载到我的 RecyclerView 中,但在尝试从 body 对象中获取数组后,它会出现错误。

{
  "body": {
    "posters": [
      {
        "code": "3292d2r",
        "attributes": [
          "12-plus-trailer",
          "2d",
          "adventure",
          "dubbed",
          "dubbed-lang-hu",
          "fantasy"
        ],
        "featureTitle": "Artemis Fowl",
        "weight": 0,
        "dateStarted": "2020-05-28T00:00:00"
      }
    ]
  }
}

我的 Java 代码:

public void onResponse(String response) {
    progressDialog.dismiss();

    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray jsonArray = jsonObject.getJSONObject("body").getJSONArray("posters");

但是这种方法不起作用,有什么办法可以将 posters 中的项获取到一个 JSON 数组中吗?

JSON 文件结构:

{
  "body": {
    "posters": [
      {},
      {},
      {},
      {},
      {},
      {},
      {},
      {},
      {},
      {},
      {},
      {},
      {},
      {}
    ]
  }
}
英文:

This is my json file. I am trying to load json in to my recyclerview, but after trying to get the array from the body object it runs on error.

{
body: {
posters: [
{
code: "3292d2r",
attributes: [
"12-plus-trailer",
"2d",
"adventure",
"dubbed",
"dubbed-lang-hu",
"fantasy"
],
featureTitle: "Artemis Fowl",
weight: 0,
dateStarted: "2020-05-28T00:00:00" 

My java code:

public void onResponse(String response) {
                    progressDialog.dismiss();

                try {
                    JSONObject jsonObject=new JSONObject(response);
                    JSONArray jsonArray=jsonObject.getJSONArray("body");

But this method doesn't work, any idea how I can get the items in posters to a JSONARRAY?
The JSON file structure:

{
body: {
posters: [
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
]
}
}

答案1

得分: 0

body不是一个JSONArray,它是一个JSONObject。

jsonObject.getJSONObject("body").getJSONArray("posters");
英文:

body is not a JSONArray. It is a JSONObject.

jsonObject.getJSONObject("body").getJSONArray("posters");

huangapple
  • 本文由 发表于 2020年4月11日 02:18:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61146281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定