Parsing JSONObject having dynamic key

后端 未结 4 1566
感情败类
感情败类 2021-01-03 07:34

I have following JSON as response from my server. At first, I thought, it was invalid JSON but after validating it, it seems to be correct:

JOSN: {
    \"cat         


        
4条回答
  •  -上瘾入骨i
    2021-01-03 08:26

    But how do I parse the JSON I have here?

    if keys inside category JSONObject is dynamic then use JSONObject.keys() to get Iterator for getting values as:

    JSONObject mainJSONObj=new JSONObject();
    // get category JSONObject from mainJSONObj
    JSONObject categoryJSONObj=mainJSONObj.getJSONObject("category");
    
    // get all keys from categoryJSONObj
    
    Iterator iterator = categoryJSONObj.keys();
      while (iterator.hasNext()) {
        String key = iterator.next();
        Log.i("TAG","key:"+key +"--Value::"+categoryJSONObj.optString(key);
      }
    

提交回复
热议问题