Analyzing Web Site Specific JSON Data and Viewing in TextView

advertisements

I am looking to pull data from my websites JSON url and display only one object in the textview. I was able to parse the entire JSON array, but not the specific object.

Here's the JSON on the site:

{"id":3,"day":" an A","created_at":"2013-11-06T12:30:59.023Z","updated_at":"2013-11-06T12:30:59.023Z"}

As you can see, it's pretty simple, but basically all I want to pull is the

"day":" an A"

and display it in my textview as "an A". Until now, I've only been able to pull the entire array.

A reference to this or any solution would be greatly appreciated!

Thanks in advance!

MainActivity Class:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.parseDay);
    TextView textView1 = null;

    try {
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try{
        JSONObject json=new JSONObject("day");
        try {
        String day =json.getString("day");
        textView1.setText(day);
        } catch (JSONException e) {
        e.printStackTrace();
        }

    }
    //catch (JSONException e) {
         // e.printStackTrace();
        //}
    catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

and My GetMethod:

public class GetMethod {

public String getInternetData() throws Exception {
    BufferedReader in = null;
    String data = null;
    try {
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://www.xelatechnologies.com/hfdays/show.json");

        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while ((l = in.readLine()) !=null) {
            sb.append(l + nl);
        }

        in.close();
        data = sb.toString();
        return data;
    }
    finally {
        if (in !=null){
            try{
                in.close();
                return data;
            }catch (Exception e){
                e.printStackTrace();

            }
        }

    }
}

I'm extremely new to JSON parsing so I'm sure it is not right at all. But it's worth a try!


The JSON you posted is a JSONObject. In java you can put that object into an JSONObject like this (You can use any serialize/deserializer you would like, many exist, for this example try org.json):

String json = "{\"id\":3,\"day\":\" an A\",\"created_at\":\"2013-11-06T12:30:59.023Z\",\"updated_at\":\"2013-11-06T12:30:59.023Z\"}";
JSONObject jsonObject = new JSONObject(json);

Now you have created a json object. Next you want to set the text on the text view. The key to get your value in this case is "day". So now all you have to do is use the provided getString(String value) method on the json object.

final String DAY = "day";
String dayValue= "";
try {
    value = jsonObject.getString(DAY);
} catch (JSONException e) {
    e.printStackTrace();
}
TextView textView = findViewById(R.id.text_view);
textView.setText(dayValue);