0

I want to ask about JSON parsing. If I have a web service like this from url = http://localhost/bus/format/json:

{
  "status":"success",
  "sessionid":"489GHJ8969"
  "schedule":{
     "went":{
        "description":"this is bus schedule",
        "bus":{
           "1":"A30",
           "2":"A40"
        },
        "depart":{
           "1":"8AM",
           "2":"11AM"
        }
      }
    }
}

how to get and parse this JSON in Android then show it to textview? I had search in here but I still am confused how to parse JSON.

This is update after i tried to parse the JSON but still no clue about how to parse JSON

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(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
    return jObj;

    }
}

bus activity

public class Bus extends Activity {

String url, s;

  //private static final String TAG_STATUS = "Status";
private static final String TAG_SCHEDULE = "schedule";
private static final String TAG_WENT = "went";
private static final String TAG_DESCRIPTION = "description";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jadwal);      

    TextView tx = (TextView)findViewById(R.id.textView1);

    url = "http://localhost/bus/format/json";

    tx.setText(url);

    JSONParser jParser = new JSONParser();

    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        JSONObject jadwal = json.getJSONObject(TAG_SCHEDULE);
        JSONObject went = jadwal.getJSONObject(TAG_WENT);
        s = went.getString(TAG_DESCRIPTION);

    } catch (JSONException e) {
        e.printStackTrace();
    }
}    
}

and then the result

10-13 00:51:15.212: W/System.err(373): Caused by: java.net.ConnectException: /127.0.0.1:80 - Connection refused
10-13 00:51:15.212: W/System.err(373):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:254)
10-13 00:51:15.212: W/System.err(373):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
10-13 00:51:15.212: W/System.err(373):  at java.net.Socket.connect(Socket.java:1055)
10-13 00:51:15.222: W/System.err(373):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-13 00:51:15.222: W/System.err(373):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
10-13 00:51:15.222: W/System.err(373):  ... 21 more
10-13 00:51:15.222: E/Buffer Error(373): Error converting result java.lang.NullPointerException
10-13 00:51:15.222: E/JSON Parser(373): Error parsing data org.json.JSONException: End of input at character 0 of 
10-13 00:51:15.222: D/AndroidRuntime(373): Shutting down VM
10-13 00:51:15.222: W/dalvikvm(373): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
3

2 Answers 2

2

look at http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
For Example:

JSONObject object = new JSONObject(yourResponse);
String status = object.getString("status");
JSONObject schedule = object.getJSONObject("schedule");
.....
0

Android has JSON parser built in: http://developer.android.com/reference/org/json/package-summary.html

It wirks but utilizes DOM model ( and thus not suitable for bigger data ) If you need more comfort or databinging gran google GSON or use my small library:

https://github.com/ko5tik/jsonserializer

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.