【发布时间】:2019-11-12 07:53:16
【问题描述】:
我从我的 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 $
我错过了什么? data 实际上是一个数组而不是一个对象
【问题讨论】:
-
使用
gson库序列化或反序列化github.com/google/gson -
请查看更新后的帖子