【问题标题】:Value of type org.json.JSONArray cannot be converted to JSONObjectorg.json.JSONArray 类型的值无法转换为 JSONObject
【发布时间】:2013-12-13 07:13:09
【问题描述】:

遇到这个错误:

3169-3190/com.meisolsson.app E/JSON Parser﹕解析数据时出错 org.json.JSONException: Value [{"type":0,"can_see_custom_stories":true,"display":"","name ":"jenniaim"},{"type":0,"can_see_custom_stories":true,"display":"","name":"lucasgardebrand"},{"type":0,"can_see_custom_stories":true," display":"","name":"herr_anderzzon"},{"type":0,"can_see_custom_stories":true,"display":"","name":"chrillebile"},{"type":0 ,"can_see_custom_stories":true,"display":"","name":"meisolsson"},{"type":0,"can_see_custom_stories":true,"display":"","name":"sakanapanda" },{"type":0,"can_see_custom_stories":true,"display":"Team Snapchat","name":"teamsnapchat"},{"type":0,"can_see_custom_stories":true,"display": "","name":"fabianlindfors"},{"type":0,"can_see_custom_stories":true,"display":"Katja","name":"katjaawesome"},{"type":0," can_see_custom_stories":true,"display":"","name":"swimkey"},{"type":1,"can_see_custom_stories":true,"display":"","name":"agnesholmberg"}] org.json.JSONArray 类型的无法转换为 JSO对象

代码如下:

public class JSONParser extends AsyncTask<Void, Void, Boolean> {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", LoginActivity.Suser));
        nameValuePairs.add(new BasicNameValuePair("password", LoginActivity.Spass));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return  jObj;

}
@Override
protected Boolean doInBackground(Void... params) {
    getJSONFromUrl("http://flapplabs.se/development/snapchat/friends.php");
    return null;
}
}

【问题讨论】:

  • 您对异常有什么不明白的地方?
  • 这是一个json数组,里面没有对象所以不能转换
  • JSON 的东西我很不擅长,谢谢你的帮助

标签: java android json


【解决方案1】:

[..] 表示应该是JSONArray{..} 表示应该是JSONObject

因此:

try {
        JSONArray jObj = new JSONArray(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

【讨论】:

  • 你刚刚救了我的命❤❤ ummuah
【解决方案2】:

即使我有一个 JSON 对象,它也被存储在一个数组中 Hariharan 用方括号 [ { items:items, ... } ]

指出

我的解决方案是简单地解析出唯一的对象,像这样位于数组位置 [0] 中

JSONArray jsonArray = new JSONArray(stringIn);

JSONObject obj = jsonArray.getJSONObject(0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2018-02-04
    • 2019-12-29
    • 2013-06-30
    相关资源
    最近更新 更多