2016-12-14 13 views
-4
**https://wpsvc5.com/ESAWebAPI/DwgData/018/1821%20Cedar%20Pkwy/Floor%201_BGD.json 

これは私がindex(2)の配列値を取得するためのjsonファイルです。私は文字列で完全なjsonファイルデータとしてjsonの文字列を取得していますが、私はjsonオブジェクトに変換するときに最初のインデックスデータのみを表示しています。私はすべてのデータをオブジェクトとして必要としました。私はjson構文解析のために新しいです。ここで **json文字列cantが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

"jObj = new JSONObject(json.substring(json.indexOf(" {")、json.lastIndexOf("} ")+1));私は "{" MinX ":0、" MaxX ":5150.5、" MinY ":0、" MaxY ":2662.5、" ProjectCode ":" 018 "}"これを出力として取得しています。そして、私がちょうど "jObj =新しいJSONObject(json);を使うと、値がオブジェクトとして変換できないという例外が発生しています。 – Shweta

答えて

-2

json =" {\ "Data \": "+ json +"} ";

jObj = new JSONObject(json);

私はこのような私のjsonの文字列をちょうどconcatinatedし、それは私のために働く。

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"、」ユーザ名 ":" chand "、" user_phone ":" 9620085675 "、" re f_id ":6386}}

1

Shwetaさん、あなたはJSONArrayレスポンスを取得し、あなたのJsonObjectにJsonArrayが間違っていることをしようとしている

JSONArray jsonArray=new JSONArray(json); 

第2に、このようなjson形式で追加するような質問に答えることができますが、これは適切ではありませんが機能します。

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

あなたがJSONArrayを使用して、そのURLから取得したデータは非常に巨大であるdata.And 1つの以上の提​​案を解析し、それが、あるため大きなデータのアプリケーションを停止するだけで10 jsonObjectsを取得することにより、ページネーションを使用することができていることを確認します1回のリクエストでアプリケーションのパフォーマンスが向上します。

+0

nithinありがとう、しかし私は一度にすべてのデータをフェッチする必要があります。私のアプリの必要があります。私たちができることは他にありますか? – Shweta

+0

これらの参考文献を試してみてください。[link1](https://medium.com/@etiennelawlor/pagination-with-recyclerview-1cb7e66a502b#.75myrenpe)[link2](https://github.com/MarkoMilos/このプロセスは、提供されたチュートリアルで実装されました。 – Nithin

+1

実際には、これは線、円弧、円、テキストのようなすべてのデータを描画しています。ページングは​​できません。 – Shweta

関連する問題