1

我正在尝试将 json 值解析到我的 Android 应用程序中。在这里,我被困在一个需要在 json 数组列表中包含对象长度的地方。

下面是我的json:

    "events": { //json object
            "2012-11-30": [ // json array
                {
                    "privacy": "OPEN",
                    "name": "LOGDGE"   
                }
            ],
            "2013-08-17": [
                {
                    "privacy": "OPEN",
                    "name": "Party: Dinner and Dancing"
                }
            ],
            "2013-09-14": [
                {
                    "privacy": "OPEN",
                    "name": "Party: Dinner and Dancing"
                }
            ],
            "2013-09-27": [
                {
                    "privacy": "OPEN",
                    "name": "Salsa Party!"  
                }

            {
             "privacy": "OPEN",
                     "name": "Dance Performance Course",
            }       
            ],
            "2013-10-23": [
                {
                    "privacy": "OPEN",
                    "name": "Dance Performance Course"
                }
            ]
    }

现在我的问题是如何解析以获取 json 数组的长度,例如:

  • 2012-11-30 计数 = 1
  • 2013-08-17 计数 = 1
  • 2013-09-27 计数 = 2

如何使用日期循环 json 数组,因为它们是 json 对象并找到每个 json 数组日期的长度。我想在这里单独计算每个数组。我是否需要在 json 数组“日期”中添加一个键值对,以便我可以有一个 for 循环来获取数组中值的计数?

先感谢您

4

4 回答 4

6

尝试下面的代码并使用 JsonArray 的长度计算对象:

Iterator<Object> keys = eventobject.keys();
while (keys.hasNext()) {
  String key = keys.next();
    try{
         JSONArray array = jObject.getJSONArray(key);
         //count objects values using array length.
    }
    catch(Exception e){
        e.printStackTrace()
    }
}
于 2013-12-31T04:46:03.573 回答
1
String jstr = jso.getString("Events");
JSONObject mdcalkey = new JSONObject(jstr);
Iterator iter = mdcalkey.keys();
ArrayList<String> arr = new ArrayList<String>();
ArrayList<String> arrTimeDate = new ArrayList<String>();
ArrayList<String> arrtype = new ArrayList<String>();

while (iter.hasNext()) {
    arr.add((String) iter.next());
}

// ja = mdcalkey.getJSONArray(arr.get(0));
jar = new JSONArray[arr.size()];

此 arr 数组将显示所有日期数组。请检查它。如果有任何查询,请告诉我。

于 2013-12-30T07:03:12.643 回答
0
  String json =  "events": { //json object
        "2012-11-30": [ // json array
            {
                "privacy": "OPEN",
                "name": "LOGDGE"   
            }
        ],
        "2013-08-17": [
            {
                "privacy": "OPEN",
                "name": "Party: Dinner and Dancing"
            }
        ],
        "2013-09-14": [
            {
                "privacy": "OPEN",
                "name": "Party: Dinner and Dancing"
            }
        ],
        "2013-09-27": [
            {
                "privacy": "OPEN",
                "name": "Salsa Party!"  
            }

        {
         "privacy": "OPEN",
                 "name": "Dance Performance Course",
        }       
        ],
        "2013-10-23": [
            {
                "privacy": "OPEN",
                "name": "Dance Performance Course"
            }
        ]
}
       try {
                JSONObject  obj = new getJSONObject(new JSONObject("{"+"json"+"}").getJSONObject("events").toString());
                Iterator iterator = obj.keys();
          while (iterator.hasNext())
         {
          String key = (String) iterator.next();
          JSONArray array = obj.optJSONArray(key);
          if (array != null)
         {
        // the value for this key is an array
          for (int i = 0; i < array.length; i++){
          // do stuff
           }
         }
         }
          }
       catch (JSONException e)
            {
            // handle exception here
             }
于 2013-12-30T06:48:44.907 回答
0

您可以遍历 JSONObject 中的键并检查它们对应的值是否为 JSONArray,然后将它们的长度存储在映射中:

Iterator iter = yourJSONObject.keys();
while (iter.hasNext())
{
    String key = (String) iter.next();
    JSONArray array = yourJSONObject.optJSONArray(key);
    if (array != null)
    {
        // the value for this key is an array
        for (int i = 0; i < array.length; i++){
            // do stuff
        }
    }
}
于 2013-12-30T06:55:41.247 回答