gpt4 book ai didi

android - 在 Android 中解析 JSON 对象

转载 作者:行者123 更新时间:2023-11-29 17:32:24 25 4
gpt4 key购买 nike

我正在尝试解析一个如下所示的 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) error: incompatible types: int cannot be converted to String.

最佳答案

应该是

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

关于android - 在 Android 中解析 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32321731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com