0

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)
4
  • Where you get this JSON response? From any link or you created JSON format text. Commented Dec 3, 2011 at 8:59
  • no i get this from aremote server as a post.. Commented Dec 3, 2011 at 9:12
  • Try and print out what you get in the 'result1' string: it seems like the error is there. Commented Dec 3, 2011 at 9:16
  • i tried i get the same string Commented Dec 3, 2011 at 9:16

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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...

1 Comment

@freshDroid Edited my answer..Check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.