0

I would like to extract into two separate array the key "desc" and "type" in my JSON file. I get the file from an ftp site and I don't know how many entry there are. After I have the String Array, I would like to fill a Spinner by the "desc" value. How can do that?

this is my JSON file

{
"Pagnerine":[{
        "Cialda":[{
                    "userId":1,
                    "desc":"Sottozero/Estate",
                    "type":"ct"
                    },
                    {
                    "userId":2,
                    "desc":"Piccolo/Primavera",
                    "type":"ct"
                    },
                    {
                    "userId":3,
                    "desc":"Medio",
                    "type":"ct"
                    },
                    {
                    "userId":4,
                    "desc":"Grande",
                    "type":"ct"
                    }
                ],
        "Cartone":[{
                    "userId":1,
                    "desc":"16B",
                    "type":"ct"
                    },
                    {
                    "userId":2,
                    "desc":"17",
                    "type":"ct"
                    },
                    {
                    "userId":3,
                    "desc":"34",
                    "type":"ct"
                    },
                    {
                    "userId":4,
                    "desc":"20",
                    "type":"ct"
                    }
                ]
            }
        ],
"Cucchiaini":[],
"Vaschette":[],
"Zuccheri":[],
"versione":"100"
}

i have tried to implement this code for obtain how many entry (desc or type) there are, but fail because count only the first part "Cialda" and "Cartone"

Iterator<String> iter = jObj.keys();            
                    while (iter.hasNext()) {
                        String key = iter.next();
                        try {
                            JSONArray jArray = jObj.getJSONArray(key);
                            // looping through
                            entry += jArray.length();

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
4

2 回答 2

1

我建议为此实施一个更易于维护的解决方案,以便您可以轻松使用 POJO 并适应以后的潜在变化。我的首选方法是使用 [GSON}( https://code.google.com/p/google-gson/ ),但也有Jackson

请查看GSON 用户指南以了解详细信息,但基本上是这里的概念。

假设我的 JSON 如下:

JSON

{ "items" : 
  [
    {
      "type" : "food",
      "name" : "Tacos"
    },
    {
      "type" : "food",
      "name" : "Bacon"
    },
    {
      "type" : "food",
      "name" : "Beer"
    },
  ]
}

我将创建以下对象:

项目.java

public class Items {
  List<Item> items;
}

项目.java

public class Item {
  String type;
  String name;
}

然后,一旦我得到 JSON,我只需执行以下操作来创建我的 Items 对象。

Items deliciousFoodStuffs = gson.fromJson(json, Items.class);   

这将导致我将我的 JSON 反序列化为 POJO,我可以用它来满足我的需求。

假设我不想通过字段名称进行映射,而是映射到不同名称的 Java 字段,我将改为执行以下操作:

项目.java

public class Item {
  @SerializedName("type")
  String typeValue;
  @SerializedName("name")
  String nameValue;
}

使用与以前相同的调用

Items deliciousFoodStuffs = gson.fromJson(json, Items.class);   

我将收到相同的结果,期望我的变量命名不同。

注意:啤酒、培根和炸玉米饼都很美味,不一定按美味顺序排列。

于 2013-10-16T18:24:22.970 回答
1

您发布的代码与您在问题中的目标不符。但是要回答为什么entry只计算前两个元素的问题(并假设 jObj 是JSONObject包含该元素的有效Pagnerine元素)试试这个:

for(int i = 0; i < jObj.length(); i++) {
    JSONObject childObject = new JSONObject(jObj.get(i).toString());
    entry += childObject.length();
}

entry现在应该包含 JSON 文件中的元素总数。更改是遍历每个父元素(在本例中为CialdaCartone),并为每个元素计算子元素的数量。

编辑 根据您的评论,这可以轻松修改以计算父母和孩子的数量:

for(int i = 0; i < jObj.length(); i++) {
    parentCount += jObj.length();
    JSONObject childObject = new JSONObject(jObj.get(i).toString());
    childCount += childObject.length();
}
于 2013-10-16T18:20:15.683 回答