【问题标题】:How to parse JSON from localhost?如何从本地主机解析 JSON?
【发布时间】:2013-11-28 15:42:46
【问题描述】:

我正在尝试解析来自本地主机的数据,即 [{"data":{"statues":"2"}}]。

这是我的 JSON 代码:

JSONArray jArray = new JSONArray(result);
             for(int i=0;i<jArray.length();i++){
                     JSONObject json_data = jArray.getJSONObject(i);
                     Log.i("log_tag","statues: "+json_data.getString("statues"));
                     //Get an output to the screen
                    returnString = json_data.getString("statues");
                     tv.setText(returnString);

一切听起来都不错,但在我的 log-cat 中,出现了这个错误:

11-15 09:07:27.403: E/log_tag(3037): Error in http connection!!org.json.JSONException: Value [{"data":{"statues":"2"}}] of type org.json.JSONArray cannot be converted to JSONObject

我说这听起来不错,因为它捕捉到了雕像,但不是它的值(即 2)。 我也试过json_data.getInt("statues");,但问题还是一样。

我该怎么办?!

【问题讨论】:

    标签: android json localhost android-logcat


    【解决方案1】:

    试试这个..

    for(int i=0;i<jArray.length();i++){
                         JSONObject json_data = jArray.getJSONObject(i);
                         JSONObject c = json_data.getJSONObject("data");
                         Log.i("log_tag","statues: "+c.getString("statues"));
                         //Get an output to the screen
                        returnString = c.getString("statues");
                         tv.setText(returnString);
    
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用JsonConvert.DeserializeObject&lt;&gt;()

      看看这个: How can I parse JSON with C#?

      【讨论】:

        【解决方案3】:

        你必须像这样得到JSONObject

        for(int i=0;i<jArray.length();i++){
           JSONObject c = jArray.getJSONObject(i);
        
            // Storing each json item in variable
            String id = c.getString("identifier1");
            String name = c.getString("identifier2");
        
        }
        

        更多详情,您可以使用link

        【讨论】:

          猜你喜欢
          • 2016-01-09
          • 2021-08-03
          • 1970-01-01
          • 1970-01-01
          • 2012-10-27
          • 2016-07-05
          • 1970-01-01
          • 2014-02-26
          • 1970-01-01
          相关资源
          最近更新 更多