JSON Parser:解析数据时出错org.json.JSONException:org.json.JSONObject $ 1类型的值null无法转换为JSONObject

时间:2016-03-14 18:33:53

标签: android json parsing jsonexception

当我执行下面给出的代码时,它会显示一个像

这样的异常
  

" JSON Parser:解析数据时出错org.json.JSONException:Value null of   类型org.json.JSONObject $ 1无法转换为JSONObject"

这里有人可以帮助我吗?

public class JSONParser {

static JSONArray jarray = new JSONArray();
static JSONObject jobj = new JSONObject();


public JSONArray getJSONFromUrl(String url) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }



        } else {
            Log.e("==>", "Failed to download file");

        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Parse String to JSON object
    try {

        jobj = new JSONObject(String.valueOf(builder));
        jarray =    new JSONArray(jobj);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    // return JSON Object
    return jarray;

}

}

并且尝试从服务器获取的数据是

{"eventid":"1","name":"Hacking","text":"6th-7th feb.Faculty i3India        tech","date":"6th-7th","time":"9","venue":"MMH","contact":"9033637371","fee":"800"}

1 个答案:

答案 0 :(得分:0)

根据你的代码,你期待JsonArray,但你的数据示例不包含任何数组(我认为你把它错了)。检查数组中是否没有空值。