我无法从json api获取数据。多次尝试但结果不成功。这是json模式:
{
"All": [{
"MainCategory": {
"AllData": [{
"SubCategory": {
"AllData": [{
"MainBigThumb": "rsz_0_800_CHO1.jpg",
"MainImage": "rsz_0_400_CHO1.jpg",
"MainSmallImages": "rsz_0_100_CHO1.jpg"
}, {
"MainBigThumb": "rsz_0_800_CHO100.jpg",
"MainImage": "rsz_0_400_CHO100.jpg",
"MainSmallImages": "rsz_0_100_CHO100.jpg"
},
{
"MainBigThumb": "rsz_0_800_CHO101.jpg",
"MainImage": "rsz_0_400_CHO101.jpg",
"MainSmallImages": "rsz_0_100_CHO101.jpg"
}
],
"Attribute_Name": "Chocolate",
"Attribute_Value": "123",
"StockKeepingunit": null
}
},
{
"SubCategory": {
"AllData": [{
"MainBigThumb": "rsz_0_800_CRI100.jpg",
"MainImage": "rsz_0_400_CRI100.jpg",
"MainSmallImages": "rsz_0_100_CRI100.jpg"
},
{
"MainBigThumb": "rsz_0_800_CRI101.jpg",
"MainImage": "rsz_0_400_CRI101.jpg",
"MainSmallImages": "rsz_0_100_CRI101.jpg"
}
],
"Attribute_Name": "Crisps",
"Attribute_Value": "124",
"StockKeepingunit": null
}
}
],
"Attribute_Name": "Confectionery",
"Attribute_Value": "122",
"StockKeepingunit": null
}
},
{
"MainCategory": {
"AllData": [{
"SubCategory": {
"AllData": [{
"MainBigThumb": "rsz_0_800_YP100.jpg",
"MainImage": "rsz_0_400_YP100.jpg",
"MainSmallImages": "rsz_0_100_YP100.jpg"
}],
"Attribute_Name": "Sub-Category",
"Attribute_Value": "163",
"StockKeepingunit": null
}
}],
"Attribute_Name": "Your Category",
"Attribute_Value": "162",
"StockKeepingunit": null
}
}
],
"status": {
"message": "success",
"result": 1
}}
数据格式类似于
1. MainCategory1 1.1 Subcategory1 1.1.1 Product1 1.1.2 Product2 1.2 Subcategory2 1.2.1 Product1 1.2.2 Product2 2. MainCategory2 2.1 Subcategory1 2.1.1 Product1 2.1.2 Product2
答案 0 :(得分:0)
您可以使用它进行改造并将整个json数据转换为Gson,这将使它更容易。
首先复制API的响应并通过
创建bean类this site *点击源类型为JSON *注释样式为GSON *取消使用DoubleNumbers *取消AllowAdition属性
然后将这些文件复制到您的项目
然后在你的活动中
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIServiceInterface apiController = retrofit.create(APIServiceInterface.class);
Call<Bean> result = apiController.studentLogin(username,password);
result.enqueue(new Callback<Bean>() {
@Override
public void onResponse(Call<Bean> call, Response<Bean> response) {
// you will get the reponse in the response parameter
}
@Override
public void onFailure(Call<Bean> call, Throwable t) {
}
});
APIServiceInterface是接口文件。
答案 1 :(得分:0)
这很简单,但在解析{}
时,您必须对[]
,,
和Json
保持谨慎。我不会为你编写解析代码,而只是给出一个想法。
Intitial Json Response Object
Exract JsonArray named as "All" from above object
Get first Position Of above array, you will get an JsonObject
Extract a JsonObject from above object with name "MainCategory"
Exract JsonArray named as "AllData" from above object
Get first Position Of above array, you will get an JsonObject
Extract a JsonObject from above object with name "SubCategory"
Exract JsonArray named as "AllData" from above object
Get first Position Of above array, you will get an JsonObject
Containing following data,
"MainBigThumb": "rsz_0_800_CHO1.jpg",
"MainImage": "rsz_0_400_CHO1.jpg",
"MainSmallImages": "rsz_0_100_CHO1.jpg"
答案 2 :(得分:0)
我解决了我的问题,这里是json解析如下。
JSONObject jsonObj = new JSONObject(json);
JSONArray AllJsonArray = jsonObj.getJSONArray("All");
for (int i = 0; i < AllJsonArray.length(); i++) {
JSONObject AllJsonObject = AllJsonArray.getJSONObject(i);
JSONObject mainCategoryJsonObject = AllJsonObject.getJSONObject("MainCategory");
String mainCategoryName = mainCategoryJsonObject.getString("Attribute_Name");
String mainCategoryValue= mainCategoryJsonObject.getString("Attribute_Value");
JSONArray AllData1JsonArray = mainCategoryJsonObject.getJSONArray("AllData");
for (int j = 0; j < AllData1JsonArray.length(); j++) {
JSONObject AllData1JsonObject = AllData1JsonArray.getJSONObject(j);
JSONObject subCategoryJsonObject = AllData1JsonObject.getJSONObject("SubCategory");
String subCategoryName = subCategoryJsonObject.getString("Attribute_Name");
String subCategoryValue = subCategoryJsonObject.getString("Attribute_Value");
JSONArray AllData2JsonArray = subCategoryJsonObject.getJSONArray("AllData");
for (int k = 0; k < AllData2JsonArray.length(); k++) {
JSONObject AllData2JsonObject = AllData2JsonArray.getJSONObject(k);
String MainBigThumb = AllData2JsonObject.getString("MainBigThumb");
String MainImage = AllData2JsonObject.getString("MainImage");
String MainSmallImages = AllData2JsonObject.getString("MainSmallImages");
}
}
}