类型org.json.JSONArray价值不能转化为的JSONObject转化为、类型、价值、org

由网友(祸害苍生)分享简介:卡住了在此错误:3169-3190 / com.meisolsson.app E / JSON解析器:错误解析数据org.json.JSONException:值[{"type":0,"can_see_custom_stories":true,"display":"","name":"jenniaim"},{"type...

卡住了在此错误:

  

3169-3190 / com.meisolsson.app E / JSON解析器:错误解析数据org.json.JSONException:值[{"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不能转换为JSONObject的

因此​​,这里是code:

 公共类JSONParser扩展的AsyncTask<虚空,虚空,布尔> {

静态的InputStream是= NULL;
静态的JSONObject jObj = NULL;
静态JSON字符串=;

//构造
公共JSONParser(){

}

公众的JSONObject getJSONFromUrl(字符串URL){

    //使HTTP请求
    尝试 {
        // defaultHttpClient
        DefaultHttpClient的HttpClient =新DefaultHttpClient();
        HttpPost httpPost =新HttpPost(URL);

        ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(2);
        nameValuePairs.add(新BasicNameValuePair(用户名,LoginActivity.Suser));
        nameValuePairs.add(新BasicNameValuePair(密码,LoginActivity.Spass));
        httpPost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));

        HTT presponse HTT presponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = HTT presponse.getEntity();
        是= httpEntity.getContent();

    }赶上(UnsupportedEncodingException E){
        e.printStackTrace();
    }赶上(ClientProtocolException E){
        e.printStackTrace();
    }赶上(IOException异常E){
        e.printStackTrace();
    }

    尝试 {
        的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(
                是,ISO-8859-1),8);
        StringBuilder的SB =新的StringBuilder();
        串线= NULL;
        而((行= reader.readLine())!= NULL){
            sb.append(行+ N);
        }
        is.close();
        JSON = sb.toString();
    }赶上(例外五){
        Log.e(缓冲区错误,转换的结果错误+ e.toString());
    }

    //尝试解析字符串到一个JSON对象
    尝试 {
        jObj =新的JSONObject(JSON);
    }赶上(JSONException E){
        Log.e(JSON解析器,错误分析数据+ e.toString());
    }

    //返回JSON字符串
    返回jObj;

}
@覆盖
保护布尔doInBackground(空... PARAMS){
    getJSONFromUrl(http://flapplabs.se/development/snapchat/friends.php);
    返回null;
}
}
 

解决方案

[...] 意味着它应该是一个 JSONArray {...} 意味着它应该是一个的JSONObject

因此​​:

 尝试{
        JSONArray jObj =新JSONArray(JSON);
    }赶上(JSONException E){
        Log.e(JSON解析器,错误分析数据+ e.toString());
    }
 

深入浅析Java中的String JSONObject JSONArray List的转换

Got stuck at this error:

3169-3190/com.meisolsson.app E/JSON Parser﹕ Error parsing data 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"}] of type org.json.JSONArray cannot be converted to JSONObject

So here is the code:

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;
}
}

解决方案

[..] means it should be an JSONArray and {..} means it should be a JSONObject.

Therefore:

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

阅读全文

相关推荐

最新文章