John Nguyen
John Nguyen

Reputation: 557

Parsing multiple JSON strings

I would like to thank all the users in this community for helping me get as far as I am in my project today.

I now need your help once again. So far, I am able to establish a connection in my project from this JSON link (REMOVED FOR PRIVACY CONCERNS)

The problem is I am only able to parse one string, (firstName)

Here is my code:

public class JSONActivity extends Activity { 
static TextView http; 
HttpClient client; 
JSONObject json; 

    final static String URL = "REMOVED FOR PRIVACY CONCERNS

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        http = (TextView) findViewById(R.id.http); 
        client = new DefaultHttpClient(); 
        new Read().execute("firstName"); 

    } 

    public JSONObject getpw(String password) throws ClientProtocolException, 
            IOException, JSONException { 
        StringBuilder url = new StringBuilder(URL); 
        url.append(password); 

        HttpGet get = new HttpGet(url.toString()); 
        HttpResponse r = client.execute(get); 
        int status = r.getStatusLine().getStatusCode(); 
        if (status == 200) { 
            HttpEntity e = r.getEntity(); 
            String data = EntityUtils.toString(e); 
            JSONObject getname = new JSONObject(data); 

            return getname; 
        } else { 
            Toast.makeText(JSONActivity.this, "error", Toast.LENGTH_SHORT); 
            return null; 
        } 
    } 

    public class Read extends AsyncTask<String, Integer, String> { 

        @Override 
        protected String doInBackground(String... arg0) { 
            // TODO Auto-generated method stub 
            try { 
                json = getpw("trustme"); 
                return json.getString("firstName"); 
            } catch (ClientProtocolException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (JSONException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 

            return null; 
        } 

        @Override 
        protected void onPostExecute(String result) { 
            // TODO Auto-generated method stub 
            http.setText(result); 
        } 
    } 
} 

My question is, how can I parse multiple strings rather than just "firstName"?

Upvotes: 1

Views: 767

Answers (1)

coder
coder

Reputation: 10520

You can get it all by doing the following:

String firstname = json.getString("firstName");
String lastname = json.getString("lastName");
int checkedIn = json.getInt("checkedIn");
int updated = json.getInt("checkedindatetime");

JSONObject address = json.getJSONObject("address");
String streetaddress = address.getString("streetAddress");
String city = address.getString("city");
etc...

JSONArray phoneNumbers = json.getJSONArray("phoneNumber");
String type = phoneNumbers.getJSONObject(0).getString("type");
etc...

Hope this helps.

A good resource for looking at json, is this validator.

Upvotes: 1

Related Questions