计算Android中json数组内的json对象?

时间:2013-12-30 06:39:15

标签: android json arrays jsonobject

我正在尝试将json值解析为我的Android应用程序。在这里,我被困在一个地方,我需要在json arraylist中有对象长度。

以下是我的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 count = 1
  • 2013-08-17 count = 1
  • 2013-09-27 count = 2

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

提前谢谢

4 个答案:

答案 0 :(得分: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()
    }
}

答案 1 :(得分: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数组将显示所有日期数组..请检查它..如果有任何查询,请告诉我。

答案 2 :(得分: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
             }

答案 3 :(得分:0)

您可以迭代JSONObject中的键并检查它们的对应值是否为JSONArrays,然后将它们的长度存储在地图中:

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
        }
    }
}