我从API调用中得到以下JsonObject作为答复-
{
"$type": "VerteSearch.Core.Models.SearchResultCollection`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"count": 74,
"data": [
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "1",
"vendorId": 1,
"vendorPrefix": "VS0007",
"vendorName": "kiko+ & gg*",
"shortDescription": "High Quality modern-minimalist wooden toys. Designed in Japan.",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "kikoandgg",
"url": "https://myverte.com/kikoandgg",
"logoPicture": "https://images.myverte.com/0141614_270.png",
"logoPictureId": 141614,
"backgroundPicture": "https://images.myverte.com/0141612_270.png",
"backgroundPictureId": 141612,
"frontPicture": "https://images.myverte.com/0141609_270.png",
"frontPictureId": 141609,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 0.0,
"priceMax": 138.0,
"categoriesNames": "Toys",
"maxFavoritesCount": 2
}
},
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "3",
"vendorId": 3,
"vendorPrefix": "VS0008",
"vendorName": "Luna Rose",
"shortDescription": "Jewelry & Accessories Made From 99% Recycled Silver.",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "luna-rose",
"url": "https://myverte.com/luna-rose",
"logoPicture": "https://images.myverte.com/0129803_270.png",
"logoPictureId": 129803,
"backgroundPicture": "https://images.myverte.com/0129802_270.png",
"backgroundPictureId": 129802,
"frontPicture": "https://images.myverte.com/0129801_270.png",
"frontPictureId": 129801,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 129.0,
"priceMax": 189.0,
"categoriesNames": "Jewelry",
"maxFavoritesCount": 5
}
},
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "4",
"vendorId": 4,
"vendorPrefix": "VS0009",
"vendorName": "Lumi Charge",
"shortDescription": "The Ultimate LED Desk Lamp Charging Station",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "lumi-charge",
"url": "https://myverte.com/lumi-charge",
"logoPicture": "https://images.myverte.com/0093139_LumiLogo_270.png",
"logoPictureId": 93139,
"backgroundPicture": "https://images.myverte.com/0093137_BackgroundImage-Lumicharge_270.png",
"backgroundPictureId": 93137,
"frontPicture": "https://images.myverte.com/0093138_LumiFront1_270.png",
"frontPictureId": 93138,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 0.0,
"priceMax": 0.0,
"categoriesNames": "Electronics",
"maxFavoritesCount": 0
}
},
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "5",
"vendorId": 5,
"vendorPrefix": "VS0010",
"vendorName": "Milton&Goose",
"shortDescription": "Wooden Play Kitchens Made In The USA.",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "miltongoose",
"url": "https://myverte.com/miltongoose",
"logoPicture": "https://images.myverte.com/0002009_270.png",
"logoPictureId": 2009,
"backgroundPicture": "https://images.myverte.com/0002007_270.png",
"backgroundPictureId": 2007,
"frontPicture": "https://images.myverte.com/0002008_270.png",
"frontPictureId": 2008,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 649.0,
"priceMax": 799.0,
"categoriesNames": "Toys",
"maxFavoritesCount": 0
}
},
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "7",
"vendorId": 7,
"vendorPrefix": "VS0012",
"vendorName": "Birthstone Scents",
"shortDescription": "Perfumes and Home Fragrances Inspired by Gemstones.",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "birthstone-scents",
"url": "https://myverte.com/birthstone-scents",
"logoPicture": "https://images.myverte.com/0002257_270.png",
"logoPictureId": 2257,
"backgroundPicture": "https://images.myverte.com/0003156_270.jpeg",
"backgroundPictureId": 3156,
"frontPicture": "https://images.myverte.com/0000641_270.png",
"frontPictureId": 641,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 45.0,
"priceMax": 45.0,
"categoriesNames": "Candles,Perfume Oil",
"maxFavoritesCount": 8
}
},
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "8",
"vendorId": 8,
"vendorPrefix": "VS0013",
"vendorName": "Rampley & Co",
"shortDescription": "High Quality, Elegant Pocket Squares with Unique Designs.",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "rampley-co",
"url": "https://myverte.com/rampley-co",
"logoPicture": "https://images.myverte.com/0129814_270.png",
"logoPictureId": 129814,
"backgroundPicture": "https://images.myverte.com/0129813_270.png",
"backgroundPictureId": 129813,
"frontPicture": "https://images.myverte.com/0129812_270.png",
"frontPictureId": 129812,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 79.0,
"priceMax": 98.0,
"categoriesNames": "Pocket Squares",
"maxFavoritesCount": 1
}
},
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "9",
"vendorId": 9,
"vendorPrefix": "VS0014",
"vendorName": "Unbound",
"shortDescription": "A Sexual Wellness Company... for When You're Feeling Yourself...",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "unbound",
"url": "https://myverte.com/unbound",
"logoPicture": "https://images.myverte.com/0102324_270.png",
"logoPictureId": 102324,
"backgroundPicture": "https://images.myverte.com/0102320_270.png",
"backgroundPictureId": 102320,
"frontPicture": "https://images.myverte.com/0102314_270.png",
"frontPictureId": 102314,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 16.0,
"priceMax": 99.0,
"categoriesNames": "Vibrator,Jewelry,Lubricant,Accessories",
"maxFavoritesCount": 2
}
},
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "10",
"vendorId": 10,
"vendorPrefix": "VS0015",
"vendorName": "Stryx",
"shortDescription": "Rethinking the Meaning of Men's “Cosmetics” Products.",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "stryx",
"url": "https://myverte.com/stryx",
"logoPicture": "https://images.myverte.com/0140678_270.png",
"logoPictureId": 140678,
"backgroundPicture": "https://images.myverte.com/0140677_270.png",
"backgroundPictureId": 140677,
"frontPicture": "https://images.myverte.com/0140676_270.png",
"frontPictureId": 140676,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 29.0,
"priceMax": 29.0,
"categoriesNames": "Cosmetics",
"maxFavoritesCount": 5
}
},
{
"$type": "VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core",
"score": 1.41421354,
"resultData": {
"$type": "VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core",
"id": "11",
"vendorId": 11,
"vendorPrefix": "VS0016",
"vendorName": "Temporary Forevers",
"shortDescription": "Goods Created for Your Daily Journey. Crafted to Last.",
"imageUrl": "https://images.myverte.com/default-image_143.png",
"imageId": 0,
"displayOrder": 1,
"seName": "temporary-forevers",
"url": "https://myverte.com/temporary-forevers",
"logoPicture": "https://images.myverte.com/0130521_270.png",
"logoPictureId": 130521,
"backgroundPicture": "https://images.myverte.com/0130520_270.png",
"backgroundPictureId": 130520,
"frontPicture": "https://images.myverte.com/0130519_270.png",
"frontPictureId": 130519,
"bannerPicture": null,
"bannerPictureId": 0,
"priceMin": 305.0,
"priceMax": 395.0,
"categoriesNames": "Accessories",
"maxFavoritesCount": 11
}
}
]
}
问题是,我只需要某个部分,这个JsonObject的更深层次的孩子。
我需要的基本上是来自resultData
变量的信息-vendorId,vendorName,shortDescription,imageURL,vendorURL。
resultData
是JsonObjects的数组,因此我需要遍历它们并列出自己的对象。
这样做的方式是什么?
编辑-
尝试了以下解决方案-
private void getVendorsList(JsonObject body) {
JsonArray dataArray = body.getAsJsonArray("data");
Gson gson = new Gson();
JSONArray jsonArray = gson.fromJson(dataArray, JSONArray.class);
List<VendorModel> vendorModelList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject resultData = jsonArray.getJSONObject(i).getJSONObject("resultData");
int id = resultData.getInt("id");
String vendorName = resultData.getString("vendorName");
String shortDescription = resultData.getString("shortDescription");
String imageUrl = resultData.getString("imageUrl");
String url = resultData.getString("url");
VendorModel vendorModel = new VendorModel(id,vendorName, shortDescription, imageUrl, url);
vendorModelList.add(vendorModel);
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.d("vendorsList", String.valueOf(vendorModelList));
}
但是我遇到了以下错误-
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $
我想念什么?数据实际上是一个数组,而不是一个对象
答案 0 :(得分:1)
使用gson的完整解决方案:
public class SOActivity extends AppCompatActivity {
String TAG = "StackOverflow";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_so);
String jsonStr = "{\"$type\":\"VerteSearch.Core.Models.SearchResultCollection`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"count\":74,\"data\":[{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"1\",\"vendorId\":1,\"vendorPrefix\":\"VS0007\",\"vendorName\":\"kiko+ & gg*\",\"shortDescription\":\"High Quality modern-minimalist wooden toys. Designed in Japan.\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"kikoandgg\",\"url\":\"https://myverte.com/kikoandgg\",\"logoPicture\":\"https://images.myverte.com/0141614_270.png\",\"logoPictureId\":141614,\"backgroundPicture\":\"https://images.myverte.com/0141612_270.png\",\"backgroundPictureId\":141612,\"frontPicture\":\"https://images.myverte.com/0141609_270.png\",\"frontPictureId\":141609,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":0,\"priceMax\":138,\"categoriesNames\":\"Toys\",\"maxFavoritesCount\":2}},{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"3\",\"vendorId\":3,\"vendorPrefix\":\"VS0008\",\"vendorName\":\"Luna Rose\",\"shortDescription\":\"Jewelry & Accessories Made From 99% Recycled Silver.\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"luna-rose\",\"url\":\"https://myverte.com/luna-rose\",\"logoPicture\":\"https://images.myverte.com/0129803_270.png\",\"logoPictureId\":129803,\"backgroundPicture\":\"https://images.myverte.com/0129802_270.png\",\"backgroundPictureId\":129802,\"frontPicture\":\"https://images.myverte.com/0129801_270.png\",\"frontPictureId\":129801,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":129,\"priceMax\":189,\"categoriesNames\":\"Jewelry\",\"maxFavoritesCount\":5}},{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"4\",\"vendorId\":4,\"vendorPrefix\":\"VS0009\",\"vendorName\":\"Lumi Charge\",\"shortDescription\":\"The Ultimate LED Desk Lamp Charging Station\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"lumi-charge\",\"url\":\"https://myverte.com/lumi-charge\",\"logoPicture\":\"https://images.myverte.com/0093139_LumiLogo_270.png\",\"logoPictureId\":93139,\"backgroundPicture\":\"https://images.myverte.com/0093137_BackgroundImage-Lumicharge_270.png\",\"backgroundPictureId\":93137,\"frontPicture\":\"https://images.myverte.com/0093138_LumiFront1_270.png\",\"frontPictureId\":93138,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":0,\"priceMax\":0,\"categoriesNames\":\"Electronics\",\"maxFavoritesCount\":0}},{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"5\",\"vendorId\":5,\"vendorPrefix\":\"VS0010\",\"vendorName\":\"Milton&Goose\",\"shortDescription\":\"Wooden Play Kitchens Made In The USA.\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"miltongoose\",\"url\":\"https://myverte.com/miltongoose\",\"logoPicture\":\"https://images.myverte.com/0002009_270.png\",\"logoPictureId\":2009,\"backgroundPicture\":\"https://images.myverte.com/0002007_270.png\",\"backgroundPictureId\":2007,\"frontPicture\":\"https://images.myverte.com/0002008_270.png\",\"frontPictureId\":2008,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":649,\"priceMax\":799,\"categoriesNames\":\"Toys\",\"maxFavoritesCount\":0}},{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"7\",\"vendorId\":7,\"vendorPrefix\":\"VS0012\",\"vendorName\":\"Birthstone Scents\",\"shortDescription\":\"Perfumes and Home Fragrances Inspired by Gemstones.\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"birthstone-scents\",\"url\":\"https://myverte.com/birthstone-scents\",\"logoPicture\":\"https://images.myverte.com/0002257_270.png\",\"logoPictureId\":2257,\"backgroundPicture\":\"https://images.myverte.com/0003156_270.jpeg\",\"backgroundPictureId\":3156,\"frontPicture\":\"https://images.myverte.com/0000641_270.png\",\"frontPictureId\":641,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":45,\"priceMax\":45,\"categoriesNames\":\"Candles,Perfume Oil\",\"maxFavoritesCount\":8}},{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"8\",\"vendorId\":8,\"vendorPrefix\":\"VS0013\",\"vendorName\":\"Rampley & Co\",\"shortDescription\":\"High Quality, Elegant Pocket Squares with Unique Designs.\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"rampley-co\",\"url\":\"https://myverte.com/rampley-co\",\"logoPicture\":\"https://images.myverte.com/0129814_270.png\",\"logoPictureId\":129814,\"backgroundPicture\":\"https://images.myverte.com/0129813_270.png\",\"backgroundPictureId\":129813,\"frontPicture\":\"https://images.myverte.com/0129812_270.png\",\"frontPictureId\":129812,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":79,\"priceMax\":98,\"categoriesNames\":\"Pocket Squares\",\"maxFavoritesCount\":1}},{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"9\",\"vendorId\":9,\"vendorPrefix\":\"VS0014\",\"vendorName\":\"Unbound\",\"shortDescription\":\"A Sexual Wellness Company... for When You're Feeling Yourself...\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"unbound\",\"url\":\"https://myverte.com/unbound\",\"logoPicture\":\"https://images.myverte.com/0102324_270.png\",\"logoPictureId\":102324,\"backgroundPicture\":\"https://images.myverte.com/0102320_270.png\",\"backgroundPictureId\":102320,\"frontPicture\":\"https://images.myverte.com/0102314_270.png\",\"frontPictureId\":102314,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":16,\"priceMax\":99,\"categoriesNames\":\"Vibrator,Jewelry,Lubricant,Accessories\",\"maxFavoritesCount\":2}},{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"10\",\"vendorId\":10,\"vendorPrefix\":\"VS0015\",\"vendorName\":\"Stryx\",\"shortDescription\":\"Rethinking the Meaning of Men's “Cosmetics” Products.\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"stryx\",\"url\":\"https://myverte.com/stryx\",\"logoPicture\":\"https://images.myverte.com/0140678_270.png\",\"logoPictureId\":140678,\"backgroundPicture\":\"https://images.myverte.com/0140677_270.png\",\"backgroundPictureId\":140677,\"frontPicture\":\"https://images.myverte.com/0140676_270.png\",\"frontPictureId\":140676,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":29,\"priceMax\":29,\"categoriesNames\":\"Cosmetics\",\"maxFavoritesCount\":5}},{\"$type\":\"VerteSearch.Core.Models.SearchResult`1[[VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core]], VerteSearch.Core\",\"score\":1.41421354,\"resultData\":{\"$type\":\"VerteSearch.Core.Models.IndexEntities.IndexedVendor, VerteSearch.Core\",\"id\":\"11\",\"vendorId\":11,\"vendorPrefix\":\"VS0016\",\"vendorName\":\"Temporary Forevers\",\"shortDescription\":\"Goods Created for Your Daily Journey. Crafted to Last.\",\"imageUrl\":\"https://images.myverte.com/default-image_143.png\",\"imageId\":0,\"displayOrder\":1,\"seName\":\"temporary-forevers\",\"url\":\"https://myverte.com/temporary-forevers\",\"logoPicture\":\"https://images.myverte.com/0130521_270.png\",\"logoPictureId\":130521,\"backgroundPicture\":\"https://images.myverte.com/0130520_270.png\",\"backgroundPictureId\":130520,\"frontPicture\":\"https://images.myverte.com/0130519_270.png\",\"frontPictureId\":130519,\"bannerPicture\":null,\"bannerPictureId\":0,\"priceMin\":305,\"priceMax\":395,\"categoriesNames\":\"Accessories\",\"maxFavoritesCount\":11}}]}";
Gson gson = new GsonBuilder().create();
ResponseWrappper response = gson.fromJson(jsonStr,ResponseWrappper.class);
for(DataWrapper dataWrapper: response.data){
Log.i(TAG,dataWrapper.resultData.toString());
}
}
class ResponseWrappper {
@SerializedName("data")
public List<DataWrapper> data;
}
class DataWrapper {
@SerializedName("resultData")
VendorModel resultData;
}
class VendorModel {
@SerializedName("id")
public int id;
@SerializedName("vendorName")
public String name;
@SerializedName("shortDescription")
public String shortDescription;
@SerializedName("imageUrl")
public String imageURL;
@SerializedName("url")
public String url;
public VendorModel(int id, String name, String shortDescription, String imageURL, String url) {
this.id = id;
this.name = name;
this.shortDescription = shortDescription;
this.imageURL = imageURL;
this.url = url;
}
@Override
public String toString() {
return "Vendor{" +
"id=" + id +
", name='" + name + '\'' +
", shortDescription='" + shortDescription + '\'' +
", imageURL='" + imageURL + '\'' +
", url='" + url + '\'' +
'}';
}
}
}
答案 1 :(得分:1)
遍历resultData
并进行解析。
private void getVendorsList(JsonObject body) {
JsonArray jsonArray = body.getAsJsonArray("data");
List<VendorModel> vendorModelList = new ArrayList<>();
if(jsonArray != null) {
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject dataObject = jsonArray.get(i).getAsJsonObject();
if(dataObject != null) {
JsonObject resultObject = dataObject.getAsJsonObject("resultData");
if(resultObject != null) {
//Manual parse
int id = resultObject.get("id").getAsInt();
String vendorName = resultObject.get("vendorName").getAsString();
String shortDescription = resultObject.get("shortDescription").getAsString();
String imageUrl = resultObject.get("imageUrl").getAsString();
String url = resultObject.get("url").getAsString();
VendorModel vendorModel = new VendorModel(id,vendorName, shortDescription, imageUrl, url);
vendorModelList.add(vendorModel);
//Parse using Gson. Also try this instead of above if your json key and model property are aligned
/*VendorModel vendorModel = new Gson().fromJson(resultObject, VendorModel.class);
vendorModelList.add(vendorModel);*/
}
}
}
}
Log.d("vendorsList", String.valueOf(vendorModelList));
}
NB:您正在实现中将org.json
与google.gson
混合在一起。
答案 2 :(得分:0)
您应该尝试使用此代码
private void getVendorsList(JSONObject body) {
try {
JSONArray dataArray = body.getJSONArray("data");
List<VendorModel> vendorModelList = new ArrayList<>();
for (int i = 0; i < dataArray.length(); i++) {
try {
JSONObject resultData = dataArray.getJSONObject(i).getJSONObject("resultData");
int id = resultData.getInt("id");
String vendorName = resultData.getString("vendorName");
String shortDescription = resultData.getString("shortDescription");
String imageUrl = resultData.getString("imageUrl");
String url = resultData.getString("url");
VendorModel vendorModel = new VendorModel(id, vendorName, shortDescription, imageUrl, url);
vendorModelList.add(vendorModel);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}