org.json.JSONArray 类型的值无法转换为 JSONObject

Posted

技术标签:

【中文标题】org.json.JSONArray 类型的值无法转换为 JSONObject【英文标题】:Value of type org.json.JSONArray cannot be converted to 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":"f***lindfors","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 的东西我很不擅长,谢谢你的帮助 【参考方案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);

【讨论】:

以上是关于org.json.JSONArray 类型的值无法转换为 JSONObject的主要内容,如果未能解决你的问题,请参考以下文章

org.json.JSONArray 类型的无法转换为 JSONObject

react-native android中出现此问题“无法转换类型为org.json.JSONArray的参数”的原因是啥?

org.json.JSONArray 无法转换为 JSONObject

org.json.JSONObject & org.json.JSONArray

Android Kotlin Volley 如何发送 JSON 数据

java怎么获取json中的list