ShadowMan
ShadowMan

Reputation: 167

Parsing a JSON object in Android

I am trying to parse a single JSON object that looks like this:

{
  "message":"Request Successful",
  "data":{
    "id":"g8nEDt",
    "name":"Twins Bazil Twins",
    "nameDisplay":"Twins Bazil Twins",
    "abv":"6.75",
    "isOrganic":"N",
    "description":"Beers",
    }
  },
  "status":"success"
}

This is the code that I am using.

public class randomBeer  extends Activity {
TextView name1;
TextView description1;
TextView abv1;
TextView ibu1;
Button Btngetdata;

//URL to get JSON Array
    private static String urlRandom = "http://api.brewerydb.com/v2/beer/random?key=mykey";

//JSON Node Names
private static final String TAG_DATA = "data";
private static final String TAG_NAME = "name";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_ABV = "abv";
private static final String TAG_IBU = "ibu";

JSONObject data = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.randombeer);
    Btngetdata = (Button)findViewById(R.id.getdata);
    Btngetdata.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            new JSONParse().execute();

        }
    });

}

private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        name1 = (TextView)findViewById(R.id.name);
        description1 = (TextView)findViewById(R.id.description);
        abv1 = (TextView)findViewById(R.id.abv);
        ibu1 = (TextView)findViewById(R.id.ibu);
        pDialog = new ProgressDialog(randomBeer.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(urlRandom);
        return json;
    }
    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // Getting JSON Array
            data= json.getJSONArray(TAG_DATA);
            JSONObject c = data.getJSONObject(0);

            // Storing  JSON item in a Variable
            String name = c.getString(TAG_NAME);

            String ibu;

            if(c.has("ibu")) {
                ibu = c.getString(TAG_IBU);
            } else {
                ibu = "No ibu value";
            }

            String abv;

            if(c.has("abv")) {
                abv = c.getString(TAG_ABV);
            } else {
                abv = "No abv value";
            }

            String description;

            if(c.has("description")) {
                description = c.getString(TAG_DESCRIPTION);
            } else {
                description = "No description available";
            }


            //Set JSON Data in TextView
            name1.setText(name);
            description1.setText(description);
            abv1.setText(abv);
            ibu1.setText(ibu);

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

    }
}

}

The problem is that it is trying to get an array but I just need and object. How can I transform this code so it gets the object instead of trying to get an array?

I tried to change

data = json.getJSONArray(TAG_DATA); 

to

data = json.getJSONObject(TAG_DATA);

but then on the line

JSONObject c = data.getJSONObject(0); 

I get an Error:

(87, 51) error: incompatible types: int cannot be converted to String.

Upvotes: 1

Views: 929

Answers (2)

Bj&#246;rn
Bj&#246;rn

Reputation: 628

It should be

data= json.getJSONObject(TAG_DATA);

instead of

data= json.getJSONArray(TAG_DATA);

in onPostExecute. It now has

  "data":{
    "id":"g8nEDt",
    "name":"Twins Bazil Twins",
    "nameDisplay":"Twins Bazil Twins",
    "abv":"6.75",
    "isOrganic":"N",
    "description":"Beers",
    }

in it. To get i.e. the "name" use

 String name = data.getString("name");

IN ADDITION:

Make sure to execute HttpGet instead of HttpPost when using this link

http://api.brewerydb.com/v2/beer/random?key=mykey

Upvotes: 2

cybersam
cybersam

Reputation: 67044

Replace this:

        // Getting JSON Array 
        data= json.getJSONArray(TAG_DATA);
        JSONObject c = data.getJSONObject(0);

with:

        // Getting JSON Object 
        data= json.getJSONObject(TAG_DATA);

Also, replace all "c" variable usages with "data".

Upvotes: 1

Related Questions