解析中的 JSON 对象Android

发布时间: 2016/6/27 14:53:37
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图解析一个单一的 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 无法转换为字符串。

解决方法 1:

它应该是

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");

另外:

请务必使用此链接时执行而不是 HttpPost公共

http://api.brewerydb.com/v2/beer/random?key=mykey
官方微信
官方QQ群
31647020