So the json is something like it,
所以json是这样的,
"stores": [
{
"amazon": []
},
{
"flipkart": {
"product_store": "Flipkart",
"product_store_logo": "http://images-api.datayuge.in/image/ZmxpcGthcnRfc3RvcmUucG5n.png",
"product_store_url": "https://price-api.datayuge.com/redirect?id=aHR0cHM6Ly9kbC5mbGlwa2FydC5jb20vZGwvbWktYTEtYmxhY2stNjQtZ2IvcC9pdG1leDl3eHh6M2FtamF0P3BpZD1NT0JFWDlXWFVTWlZZSEVUJmFmZmlkPWFydW5iYWJ1bA",
"product_price": "14999",
"product_offer": "",
"product_color": "",
"product_delivery": "3-4",
"product_delivery_cost": "0",
"is_emi": "1",
"is_cod": "1",
"return_time": "10 Days"
}
},
{
"snapdeal": []
}
]
So the non empty object like flipkart is a JsonObject but all other empty objects are array. So I am so confused about how to remove them.
因此像flipkart这样的非空对象是JsonObject,但所有其他空对象都是数组。所以我对如何删除它们感到很困惑。
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = 0; i<store_array.length(); i++){
JSONObject store = store_array.getJSONObject(i);
if (!store.getJSONObject(store.keys().next()).has("product_store")){
store_array.remove(i);
}else {
Log.i("Size :",store_array.length()+"");
}
}
But that's not working. I know I am doing this all wrong. Because it has both array and objects so i get the following error
但那不起作用。我知道我这样做是错的。因为它有数组和对象所以我得到以下错误
Value [] at amazon of type org.json.JSONArray cannot be converted to JSONObject
Need Help!
1 个解决方案
#1
1
I see two problems with your code:
我看到你的代码有两个问题:
- Your JSON structure for
"stores"
is heterogeneous — some elements have a key that maps to an array and some to an object. That's the immediate cause of the error you are seeing. You can either modify your JSON so everything key maps to an object or code defensively. - When you remove an entry, all subsequent entries move up one space, but since you then increment the loop index
i
, you skip the entry that just moved into the index you just removed. The easiest way to deal with that is to iterate throughstore_array
in reverse order.
“store”的JSON结构是异构的 - 一些元素有一个映射到数组的键,一些映射到一个对象。这是您所看到的错误的直接原因。您可以修改JSON,以便所有键都可以防御地映射到对象或代码。
当您删除一个条目时,所有后续条目都向上移动一个空格,但是由于您随后递增了循环索引i,所以跳过刚刚移入您刚删除的索引的条目。处理它的最简单方法是以相反的顺序迭代store_array。
Putting this all together (and assuming you aren't going to change your JSON structure), something like the following (untested) should work:
把这一切放在一起(并假设你不会改变你的JSON结构),类似下面的内容(未经测试)应该有效:
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = store_array.length() - 1; i >= 0; i--){
JSONObject store = store_array.getJSONObject(i);
Object storeData = store.get(store.keys().next());
boolean isValidStore = storeData instanceof JSONObject
&& ((JSONObject) storeData).has("product_store");
if (!isValidStore) {
store_array.remove(i);
}
}
#1
1
I see two problems with your code:
我看到你的代码有两个问题:
- Your JSON structure for
"stores"
is heterogeneous — some elements have a key that maps to an array and some to an object. That's the immediate cause of the error you are seeing. You can either modify your JSON so everything key maps to an object or code defensively. - When you remove an entry, all subsequent entries move up one space, but since you then increment the loop index
i
, you skip the entry that just moved into the index you just removed. The easiest way to deal with that is to iterate throughstore_array
in reverse order.
“store”的JSON结构是异构的 - 一些元素有一个映射到数组的键,一些映射到一个对象。这是您所看到的错误的直接原因。您可以修改JSON,以便所有键都可以防御地映射到对象或代码。
当您删除一个条目时,所有后续条目都向上移动一个空格,但是由于您随后递增了循环索引i,所以跳过刚刚移入您刚删除的索引的条目。处理它的最简单方法是以相反的顺序迭代store_array。
Putting this all together (and assuming you aren't going to change your JSON structure), something like the following (untested) should work:
把这一切放在一起(并假设你不会改变你的JSON结构),类似下面的内容(未经测试)应该有效:
JSONArray store_array = product_details_json.getJSONObject("data").getJSONArray("stores");
for (int i = store_array.length() - 1; i >= 0; i--){
JSONObject store = store_array.getJSONObject(i);
Object storeData = store.get(store.keys().next());
boolean isValidStore = storeData instanceof JSONObject
&& ((JSONObject) storeData).has("product_store");
if (!isValidStore) {
store_array.remove(i);
}
}