简体   繁体   中英

android:parsing through json

This is the Json string I have to parse.

[{"Vehicle":{"display_name":"Anoop","id":"9","tank_capacity":"0.00"},
"Personnel":{"display_name":null,"id":null},
"DeviceLog":{"latitude":"11.182010","longitude":"75.870165","direction":"0","place":"0","speed":"0.00","stop_time":"15245","status":"1","tank_status":"0.00","mileage":"0.00","alerts":null,"kms_left":"0.00","gps_time":"16-07-2011 10:47:23"}}]

I tried following code.My aim is to get the values retrieved from json in logcat.

JSONObject json=new JSONObject(result1);
JSONObject vehicle=json.getJSONObject("Vehicle");
JSONObject deviceLog=json.getJSONObject("DeviceLog");

String vehicleName=vehicle.getString("display_name");
int vehicleId=vehicle.getInt("id");

Double latitude=deviceLog.getDouble("latitude");
Double longitude=deviceLog.getDouble("longitude");

System.out.println(vehicleId +"  "+ vehicleName+ "  "+ latitude+"  "+longitude);

I am not sure with this code.This is what my logcat is showing.

12-03 13:36:32.983: W/System.err(693): org.json.JSONException: A JSONObject text must begin with '{' at character 1 of [{"Vehicle":{"display_name":"galaxy","id":"14","tank_capacity":"0.00"},"Personnel":{"display_name":null,"id":null},"DeviceLog":{"latitude":"9.589788","longitude":"76.530592","direction":"0","place":"Kumarakom Lake Resort(0.12km)","speed":"0.00","stop_time":"704","status":"1","tank_status":"0.00","mileage":"0.00","alerts":null,"kms_left":"0.00","gps_time":"03-12-2011 14:12:12"}}]
12-03 13:36:33.054: W/System.err(693):  at org.json.JSONTokener.syntaxError(JSONTokener.java:448)
12-03 13:36:33.054: W/System.err(693):  at org.json.JSONObject.<init>(JSONObject.java:178)
12-03 13:36:33.063: W/System.err(693):  at org.json.JSONObject.<init>(JSONObject.java:246)
12-03 13:36:33.063: W/System.err(693):  at com.rekonsult.signon.Home.createvehicleSubList(Home.java:56)
12-03 13:36:33.063: W/System.err(693):  at com.rekonsult.signon.Home.onCreate(Home.java:35)
12-03 13:36:33.063: W/System.err(693):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
12-03 13:36:33.063: W/System.err(693):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
12-03 13:36:33.063: W/System.err(693):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
12-03 13:36:33.073: W/System.err(693):  at android.app.ActivityThread.access$2100(ActivityThread.java:116)
12-03 13:36:33.073: W/System.err(693):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
12-03 13:36:33.073: W/System.err(693):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 13:36:33.073: W/System.err(693):  at android.os.Looper.loop(Looper.java:123)
12-03 13:36:33.073: W/System.err(693):  at android.app.ActivityThread.main(ActivityThread.java:4203)
12-03 13:36:33.073: W/System.err(693):  at java.lang.reflect.Method.invokeNative(Native Method)
12-03 13:36:33.073: W/System.err(693):  at java.lang.reflect.Method.invoke(Method.java:521)
12-03 13:36:33.084: W/System.err(693):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-03 13:36:33.084: W/System.err(693):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
12-03 13:36:33.084: W/System.err(693):  at dalvik.system.NativeStart.main(Native Method)

Try this:

JSONArray rec=new JSONArray(result1);

JSONObject json=rec.getJSONObject(0);
JSONObject vehicle=json.getJSONObject("Vehicle");
JSONObject deviceLog=json.getJSONObject("DeviceLog");

String vehicleName=vehicle.getString("display_name");
int vehicleId=vehicle.getInt("id");

Double latitude=deviceLog.getDouble("latitude");
Double longitude=deviceLog.getDouble("longitude");

System.out.println(vehicleId +"  "+ vehicleName+ "  "+ latitude+"  "+longitude);

Just Try this Code For Json Parsing:----

    json = JSONfunctions.getJSONfromURL(give_yourUrl);

    JSONObject vehicle=(JSONObject) json.getJSONObject("Vehicle");
    Iterator map=vehicle.keys(); 
    //Iterator iter = map.iterator();
    while(map.hasNext()){
      String key = (String)map.next();
      String value = vehicle.get(key).toString();
      Log.i("Key",""+key);
      Log.i("Value",""+value); }


  JSONObject deviceLog= (JSONObject) json.getJSONObject("DeviceLog");
  Iterator map=deviceLog.keys(); 
    //Iterator iter = map.iterator();
    while(map.hasNext()){
      String key = (String)map.next();
      String value = deviceLog.get(key).toString();
      Log.i("Key",""+key);
      Log.i("Value",""+value); }

Create JSONfunctions.java :--

public class JSONfunctions {

public static JSONObject getJSONfromURL(String url){
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }

  //convert response to string
    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();
            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }

    try{

        jArray = new JSONObject(result);            
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return jArray;

}
 }

I hope this help...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM