Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用GSON解析API中的JSON_Java_Android_Json_Gson_Retrofit2 - Fatal编程技术网

Java 使用GSON解析API中的JSON

Java 使用GSON解析API中的JSON,java,android,json,gson,retrofit2,Java,Android,Json,Gson,Retrofit2,我正在使用改型进行API调用,问题是我从调用中得到的JSON。通常它是一个简单的Json,里面有一个数组 [ { "herbID": 1, "nameTrival": "Baldrian", "nameWissenschaft": "Valeriana officinalis", .... }, { "herbID": 2, "nameTrival": "Ringelblume", "nameWissenschaft": "Cale

我正在使用改型进行API调用,问题是我从调用中得到的JSON。通常它是一个简单的Json,里面有一个数组

[
  {
    "herbID": 1,
    "nameTrival": "Baldrian",
    "nameWissenschaft": "Valeriana officinalis",
  ....
  },
  {
    "herbID": 2,
    "nameTrival": "Ringelblume",
    "nameWissenschaft": "Calendula officinalis",
  ....
  },
  ....
]
新的电话是这样的

 [
  [
    {
      "nameTrival": "Baldrian",
      "nameWissenschaft": "Valeriana officinalis",
      "hoeheFrom": 20,
      "hoeheTo": 200,
      "familie": "Baldriangewaechse",
      "pflanzentype": "Staude",
      "auffaelligkeiten": "Je nach Standort sind die Fiederblätter schmäler oder breiter sowie dunkel- oder hellgrün, oft auch unterschiedlich geformt."
    }
  ],
  [
    {
      "standort": "Ufer"
    },
    {
      "standort": "Graben"
    },
    {
      "standort": "Wiesen"
    },
    {
      "standort": "Waldrand"
    }
  ],
  [
    {
      "gebiet": "Nordeuropa"
    },
    {
      "gebiet": "Südeuropa"
    },
    {
      "gebiet": "Westeuropa"
    },
    {
      "gebiet": "Osteuropa"
    },
    {
      "gebiet": "Südosteuropa"
    },
    {
      "gebiet": "Mitteleuropa"
    },
    {
      "gebiet": "Südwesteuropa"
    },
    {
      "gebiet": "Nordosteuoropa"
    },
    {
      "gebiet": "Nordwesteuropa"
    }
  ],
  {
    "fieldCount": 0,
    "affectedRows": 0,
    "insertId": 0,
    "serverStatus": 34,
    "warningCount": 0,
    "message": "",
    "protocol41": true,
    "changedRows": 0
  }
]
public class Herb{
    ArrayList<Botanical> botanical;
    ArrayList<Location> locations;
    ArrayList<Area> areas;

    public ArrayList<Botanical> getBotanical() {
        return botanical;
    }
    public ArrayList<Location> getLocations() {
        return locations;
    }
    public ArrayList<Area> getAreas() {
        return areas;
    }
}
我用以下代码解析了第一个Json

Call<List<Herb>> call = service.getAllHerbs();

            call.enqueue(new Callback<List<Herb>>() {
                @Override
                public void onResponse(Call<List<Herb>> call, Response<List<Herb>> response) {
                    herbList = response.body();
                    loadDataList(herbList);
                }

                @Override
                public void onFailure(Call<List<Herb>> call, Throwable t) {
                    Toast.makeText(PlantListActivity.this, "Unable to load herbs\nCheck your internet connection", Toast.LENGTH_LONG).show();
                }
            });
Call Call=service.getAllHerbs();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
herbList=response.body();
加载数据列表(herbList);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(PlantListActivity.this,“无法加载草药\n检查您的internet连接”,Toast.LENGTH\u LONG.show();
}
});
新的类看起来像这样

 [
  [
    {
      "nameTrival": "Baldrian",
      "nameWissenschaft": "Valeriana officinalis",
      "hoeheFrom": 20,
      "hoeheTo": 200,
      "familie": "Baldriangewaechse",
      "pflanzentype": "Staude",
      "auffaelligkeiten": "Je nach Standort sind die Fiederblätter schmäler oder breiter sowie dunkel- oder hellgrün, oft auch unterschiedlich geformt."
    }
  ],
  [
    {
      "standort": "Ufer"
    },
    {
      "standort": "Graben"
    },
    {
      "standort": "Wiesen"
    },
    {
      "standort": "Waldrand"
    }
  ],
  [
    {
      "gebiet": "Nordeuropa"
    },
    {
      "gebiet": "Südeuropa"
    },
    {
      "gebiet": "Westeuropa"
    },
    {
      "gebiet": "Osteuropa"
    },
    {
      "gebiet": "Südosteuropa"
    },
    {
      "gebiet": "Mitteleuropa"
    },
    {
      "gebiet": "Südwesteuropa"
    },
    {
      "gebiet": "Nordosteuoropa"
    },
    {
      "gebiet": "Nordwesteuropa"
    }
  ],
  {
    "fieldCount": 0,
    "affectedRows": 0,
    "insertId": 0,
    "serverStatus": 34,
    "warningCount": 0,
    "message": "",
    "protocol41": true,
    "changedRows": 0
  }
]
public class Herb{
    ArrayList<Botanical> botanical;
    ArrayList<Location> locations;
    ArrayList<Area> areas;

    public ArrayList<Botanical> getBotanical() {
        return botanical;
    }
    public ArrayList<Location> getLocations() {
        return locations;
    }
    public ArrayList<Area> getAreas() {
        return areas;
    }
}
公共类{
ArrayList植物学;
阵列列表位置;
阵列列表区域;
公共阵列列表get植物学(){
回归植物学;
}
公共阵列列表getLocations(){
返回地点;
}
公共阵列列表getAreas(){
返回区;
}
}
对于新的Json和类,它总是失败,错误为“java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第2列路径$”处为BEGIN_数组。

在我将它们声明为ArrayList之后,我没有告诉gson下面是一个数组吗?我的课怎么了

编辑:
可能的重复和我的问题之间的区别是,另一个具有名称的数组。我的json数组没有,这就是为什么我很难解析它们。

问题是你告诉Gson你有一个属于你类型的对象。你没有。您有一个类型的对象数组。你不能只是试着把结果变成那样,然后期待它神奇地工作;)


Gson的用户解释了如何处理这个问题,你的新反应非常糟糕。解析这种JSON是不可能的

它表示不同对象(不同类型)的数组。因此,GSON什么也做不了,您必须创建一个等价于整个响应的类


您的第一个JSON响应很好。

如果您想使用类型
Herb
类处理响应,那么您需要修改服务响应,如

而不是
[{},{},{}]
//这很难处理

试试这个,这必须在服务器端完成

{
                "botanical":[],
                "area" : [],
                "location" : []
 }

然后调用将是
callsomemethod()

如果无法进行更改 然后将其作为JsonArray处理

调用someMethod()


通过这种方式,您可以处理现有的响应和获取值键,并根据需要进行解析。但不建议这样做,因为如果将来发生更改并且需要大量更改,则很难维护

您可以发布您拥有的完整JSON吗?@ReazMurshed我更新了JSON部分,现在我得到的是完整的JSON。这是一种特殊草药的详细反应。