我正在尝试解析如下所示的单个JSON对象:

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


这是我正在使用的代码。

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();
        }

    }
}

}


问题是它试图获取一个数组,但我只需要和对象。
如何转换此代码,使其获取对象而不是尝试获取数组?

我试图改变

data = json.getJSONArray(TAG_DATA);




data = json.getJSONObject(TAG_DATA);


但是然后就行了

JSONObject c = data.getJSONObject(0);


我得到一个错误:


  (87,51)错误:不兼容的类型:int无法转换为
  串。

最佳答案

它应该是

data= json.getJSONObject(TAG_DATA);


代替

data= json.getJSONArray(TAG_DATA);


在onPostExecute中。现在有

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


在里面。要获取即“名称”,请使用

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


此外:

使用此链接时,请确保执行HttpGet而不是HttpPost

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

08-06 07:19
查看更多