gpt4 book ai didi

java - Android与Json解析

转载 作者:行者123 更新时间:2023-12-01 10:34:36 26 4
gpt4 key购买 nike

我试图让我的应用程序连接到 REST API 并从中提取数据。到目前为止我已经提取了数据。但我不知道如何解析它。我相信这就是你接下来要做的。

这里是我的代码 fragment ,它连接到我的其余 API 并获取数据。我得到的错误是 JSONArray 无法转换为 JSONObject

 if (status == 200) {
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String responseString;
StringBuilder sb = new StringBuilder();

while ((responseString = reader.readLine()) != null) {
sb = sb.append(responseString);
}
String speciesListData = sb.toString();
species= SpeciesJson.fromJson(speciesListData);
Log.d(Constants.TAG, "speciesJSON: " + species);
return true;
}

这是我尝试解析它的地方,直到这里它都工作正常。她是我尝试解析它的那一行

species= SpeciesJson.fromJson(speciesListData);

这就是它坏了的原因

public class SpeciesJson {
private String scientific_name, name,description;


public SpeciesJson (JSONObject species) throws JSONException {

this.scientific_name=species.optString("scientific_name");
this.name=species.optString("name");
this.description=species.optString("description");

}
public static ArrayList<SpeciesJson> fromJson(String photoData) throws JSONException {
ArrayList<SpeciesJson> speciesData = new ArrayList<>();
JSONObject data = new JSONObject(photoData);
JSONObject photos = data.optJSONObject("name");
JSONArray photoArray = photos.optJSONArray("name");

for (int i = 0; i < photoArray.length(); i++) {
JSONObject photo = (JSONObject) photoArray.get(i);
SpeciesJson currentPhoto = new SpeciesJson(photo);
speciesData.add(currentPhoto);
}
return speciesData;
}

所以当我使用我制作的解析方法运行它时,它不起作用。

hte json 数据的示例如下,我试图在 View 中显示科学名称和名称

  {
"id": 1,
"scientific_name": "Platanus racemosa",
"name": "California Sycamore",
"description": "typically in river areas, but planted all throughout L.A",
"type": 1
},
{
"id": 2,
"scientific_name": "Pyrus kawakamii",
"name": "Ornamental Pear",
"description": "native to Asia, commonly planted in L.A",
"type": 1
},
{
"id": 3,
"scientific_name": "Liquidambar styraciflua",
"name": "American Sweetgum",
"description": "native to SE U.S, planted all around L.A",
"type": 1
},
{
"id": 4,
"scientific_name": "Setophaga coronata",
"name": "Yellow-rumped Warbler",
"description": "native bird, spends the winter in L.A before migrating north during the summer to breed",
"type": 2
},
{
"id": 5,
"scientific_name": "Calypte anna",
"name": "Anna's Hummingbird",
"description": "native bird, does not migrate. Spends the year in L.A",
"type": 2
},
{
"id": 6,
"scientific_name": "Regulus calendula",
"name": "Ruby-crowned Kinglet",
"description": "native bird, spends the winter in L.A before migrating north during the summer to breed",
"type": 2
}
]

最佳答案

我亲爱的 friend 使用谷歌GSON库就是这样。

为了您的帮助,我让这件事变得更容易了。

创建这个类SpeciesJson.java

public class SpeciesJson {

private String scientific_name;
private String name;
private String description;

public SpeciesJson() {
}

public SpeciesJson(String scientific_name,String name,String description) {
this.scientific_name = scientific_name;
this.name = name;
this.description = description;
}


//And getter,setters
}

如果 SpeciesJson 是一个简单的对象,则使用它

Gson gson = new Gson();
SpeciesJson species = gson.fromJson(responseString,SpeciesJson.class);

如果 SpeciesJson 是一个 ArrayList,则使用它(它看起来像您的情况,因此请检查它,因为您的 Json 响应包含多个 SpeciesJson 对象)

Gson gson = new Gson();
ArrayList<SpeciesJson> species = new ArrayList<>();
SpeciesJson[] speciesarray = (SpeciesJson[]) gson.fromJson(responseString,SpeciesJson[].class);
Collections.addAll(species, speciesarray);

如果您想了解有关 Gson 库的更多信息,请查看此链接 https://guides.codepath.com/android/Leveraging-the-Gson-Library

关于java - Android与Json解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34849630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com