**https://wpsvc5.com/ESAWebAPI/DwgData/018/1821%20Cedar%20Pkwy/Floor%201_BGD.json
這是我的json文件,我想獲取索引(2)的數組值。我得到的json字符串作爲一個完整的json文件數據字符串,但是當我轉換成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;
}
}
你從json得到什麼迴應?你可以表演嗎? –
while this jObj = new JSONObject(json.substring(json.indexOf(「{」),json.lastIndexOf(「}」)+ 1));「我得到「{」MinX「:0,」MaxX「:5150.5,」MinY「:0,」MaxY「:2662.5,」ProjectCode「:」018「}」這是一個輸出。而當我只使用「jObj = new JSONObject(json);」我收到異常,價值不能轉換爲一個對象。 – Shweta