json字符串无法转换为json对象

时间:2016-12-14 08:40:34

标签: android json

**https://wpsvc5.com/ESAWebAPI/DwgData/018/1821%20Cedar%20Pkwy/Floor%201_BGD.json

这是我的json文件,我想得到索引(2)的数组值。我将json字符串作为字符串中的完整json文件数据,但是当我转换为json对象时,它只显示第一个索引数据。我需要所有数据作为对象。我是json解析的新手。 **

package com.example.swetha.myapplication;

import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;


/**
 * Created by swetha on 12/13/2016.
 */

public class JSONParser {

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

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) throws JSONException {

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

            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
     /*   try {
            //jObj= new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("]")+1));//IndexOf("}") +1));
            //jObj  = new JSONArray(json).getJSONObject(1);
         //   json = json.replace("\\\"","'");
           // jObj = new JSONObject(json.substring(1,json.length()));
        } catch (JSONException e) {
            e.printStackTrace();
        }*/
        // return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
        return jObj;
    }
}

3 个答案:

答案 0 :(得分:1)

以下是一个示例如何使用它

String json = "Assuming that here is your JSON response"; 
try {
    JSONObject parentObject = new JSONObject(json);
    JSONObject userDetails = parentObject.getJSONObject("user_details"); 

    //And then read attributes like             
    String name = userDetails.getString("user_name"); 
    String phone = userDetails.getString("user_phone");
    String id = userDetails.getString("re‌​f_id");

} catch (JSONException e) {
    e.printStackTrace();
} 

以上代码适用于

{ “user_details”:`{ “USER_ID”: “1”, “USER_NAME”: “昌德”, “USER_PHONE”: “9620085675”, “再F_ID”:6386}}

答案 1 :(得分:0)

Shweta,

您正在获取JSONArray响应并且您正在尝试JsonArray到JsonObject这是错误的。

JSONArray jsonArray=new JSONArray(json);

其次,你已经回答了你的问题,比如用这样的json格式附加,这不是一个合适但有效的方法。

json = "{\"Data\":" + json + "}";
jObj = new JSONObject(json);

确保您正在使用JSONArray并解析数据。还有一个建议,从该URL获取的数据非常庞大,并且由于数据量较大可能会停止您的应用程序,通过每个请求只获取10个jsonObjects来使用分页提高应用程序的性能。

答案 2 :(得分:-2)

json =" {\"数据\":" + json +"}";

jObj = new JSONObject(json);

我刚刚将这样的json字符串连接起来,它对我有用。