2016-12-14 104 views
-4
**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; 
    } 
} 
+1

你从json得到什么回应?你可以表演吗? –

+0

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

回答

-2

json =”{\“Data \”:“+ json +”}“;

jObj = new JSONObject(json);

我刚刚concatinated我这样的json字符串,它适用于我。

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“:”chand“,”user_phone“:”9620085675“,”re f_id“:6386}}

1

Shweta,

你得到JSONArray响应和你的努力是JsonArray到的JSONObject这是不对的。

JSONArray jsonArray=new JSONArray(json); 

其次,你已经回答了你的问题,像这样的JSON格式附加,这是不正确的,但工程。

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

确保您使用的JSONArray和解析data.And一个建议,从URL获取的数据是非常巨大的,它可能会停止,因为大数据的应用程序,通过获取只有10一个JSONObjects使用分页每个请求将会提高应用程序的性能。

+0

谢谢nithin,但我必须一次获取所有数据..需要我的应用程序。还有什么我们可以做的吗? – Shweta

+0

尝试这些参考,这将为你工作。[link1](https://medium.com/@etiennelawlor/pagination-with-recyclerview-1cb7e66a502b#.75myrenpe)[link2](https://github.com/MarkoMilos/分页)。这个过程在提供的教程中实现。 – Nithin

+1

其实nithin这是绘制数据...所有像线条,弧形,圆圈,文本和更..所以不能做分页。 – Shweta