从服务器上获取一个json的数据  请大家看看 应该怎么解析 PS:格式什么的都对,请不要纠结于括号上或者json的格式上!保证是对的!!!! 听一个朋友说用foreach  但是不知道具体改怎么操作,有谁能指导下吗???? "taxonomy":{"1":{"weight":"0","tid":"1","v_weight_unused":"0","vid":"1","description":"","name":"name1"}, 
"2":{"weight":"0","tid":"2","v_weight_unused":"0","vid":"1","description":"","name":"name2"}, 
"3":{"weight":"0","tid":"3","v_weight_unused":"0","vid":"1","description":"","name":"name3"}, 
"510":{"weight":"0","tid":"510","v_weight_unused":"0","vid":"1","description":"","name":"name4"}, 
"4":{"weight":"0","tid":"4","v_weight_unused":"0","vid":"5","description":"","name":"name5"}} 

解决方案 »

  1.   

    把得到的字符串转换成JSONObject对象,再对该对象解析get("weight").toString()
      

  2.   

    测试通过,外面加了一层括号
    StringBuilder jsonString = new StringBuilder();
                jsonString.append("{\"taxonomy\":{\"1\":{\"weight\":\"0\",\"tid\":\"1\",\"v_weight_unused\":\"0\",\"vid\":\"1\",\"description\":\"\",\"name\":\"name1\"},");
                jsonString.append("\"2\":{\"weight\":\"0\",\"tid\":\"2\",\"v_weight_unused\":\"0\",\"vid\":\"1\",\"description\":\"\",\"name\":\"name2\"},");
                jsonString.append("\"3\":{\"weight\":\"0\",\"tid\":\"3\",\"v_weight_unused\":\"0\",\"vid\":\"1\",\"description\":\"\",\"name\":\"name3\"},");
                jsonString.append("\"510\":{\"weight\":\"0\",\"tid\":\"510\",\"v_weight_unused\":\"0\",\"vid\":\"1\",\"description\":\"\",\"name\":\"name4\"},");
                jsonString.append("\"4\":{\"weight\":\"0\",\"tid\":\"4\",\"v_weight_unused\":\"0\",\"vid\":\"5\",\"description\":\"\",\"name\":\"name5\"} }}");            JSONObject taxonomy;
                taxonomy = new JSONObject(jsonString.toString());
                JSONArray names = taxonomy.names();
                String name;
                for (int y = 0; y < names.length(); y++) {
                    name = (String) names.get(y);
                    Log.e("taxonomy " + name, ""+taxonomy.getJSONObject(name));
                }
      

  3.   

    先格式化json,仔细看下其构成,如下:
    根节点为taxonomy,包含5个子节点
    每个子节点包含6个属性
    "taxonomy":
    {
    "1":
    {
    "weight":"0",
    "tid":"1",
    "v_weight_unused":"0",
    "vid":"1",
    "description":"",
    "name":"name1"
    },  
    "2":
    {
    "weight":"0",
    "tid":"2",
    "v_weight_unused":"0",
    "vid":"1",
    "description":"",
    "name":"name2"
    },  
    "3":
    {
    "weight":"0",
    "tid":"3",
    "v_weight_unused":"0",
    "vid":"1",
    "description":"",
    "name":"name3"
    },  
    "510":
    {
    "weight":"0",
    "tid":"510",
    "v_weight_unused":"0",
    "vid":"1",
    "description":"",
    "name":"name4"
    },  
    "4":
    {
    "weight":"0",
    "tid":"4",
    "v_weight_unused":"0",
    "vid":"5",
    "description":"",
    "name":"name5"
    }
    }
      

  4.   

    比如,要想得到  "tid":"510",这个值,代码如下:JSONObject json = new JSONObject(...);int tid510 = json.getJSONObject("taxonomy").getJSONObject("510").getInt("tid");
      

  5.   

    json可以得到子节点的名字列表的。
      

  6.   

    JsonArray + JsonObject.
    不解释。
      

  7.   

    你是不是说的想使用递归方法自动循环遍历整个json数据?