sách gpt4 ai đã đi

Làm thế nào để truy cập đối tượng > mảng > đối tượng > mảng > đối tượng trong json?

In lại 作者:搜寻专家 更新时间:2023-11-01 08:33:05 26 4
mua khóa gpt4 Nike

我必须通过 url 中的 json 获取文本。

层次结构如下:

对象>数组>对象>数组>对象。

我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No value for text

下面是代码:-

public class ListViewActivity extends Activity {
// Log tag
private static final String TAG = ListViewActivity.class.getSimpleName();
// change here url of server api
private static final String url = "http://2e9b8f52.ngrok.io/api/v1/restaurants?per_page=5&km=1&location=true&lat=19.0558306414&long=72.8339840099";
private ProgressDialog pDialog;
private List movieList = new ArrayList();
private ListView listView;
private CustomListAdapter adapter;
@Ghi đè
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Ghi đè
public void onItemClick(AdapterView parent, View view, int position, long id) {
Movie movie = movieList.get(position);
Intent intent = new Intent(ListViewActivity.this, SecondActivity.class);
intent.putExtra("name", movie.getName());
intent.putExtra("average_ratings", movie.getAverage_ratings());
intent.putExtra("full_address", movie.getAddress());
intent.putExtra("image_url", movie.getThumbnailUrl());
intent.putExtra("cuisine",movie.getCuisine());
intent.putExtra("cost",movie.getCost());
startActivity(intent);

}
});
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Please Keep patience.Its loading...");

pDialog.show();

// Creating volley request obj
JsonObjectRequest movieReq = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener() {
@Ghi đè
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
JSONArray

restaurantsJSONArray= null;
thử {
restaurantsJSONArray = response.getJSONArray("restaurants");
} bắt (JSONException e) {
e.printStackTrace();
}

hidePDialog();
// Parsing json
for (int i = 0; i < restaurantsJSONArray.length(); i++) {
thử {

JSONObject obj =restaurantsJSONArray.getJSONObject(i);
Movie movie = new Movie();
//movie.setTitle(obj.getString("title"));
movie.setName(obj.getString("name"));
//movie.setThumbnailUrl(obj.getString("image"));
movie.setThumbnailUrl(obj.getString("org_image_url"));
movie.setAverage_ratings(obj.getString("average_ratings"));
movie.setCuisine(obj.getString("cuisine"));
movie.setAddress(obj.getJSONObject("address").getString("area"));
// movie.setAddress(obj.getJSONObject("address").getString("full_address"));
movie.setCost(obj.getString("cost"));
movie.setDistance( obj.getDouble("distance"));
movie.settext(obj.getString("text"));
movieList.add(movie);
} bắt (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, Phản hồi mới.ErrorListener() {
@Ghi đè
công khai void onErrorResponse(lỗi VolleyError) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();

}
});


AppController.getInstance().addToRequestQueue(movieReq);
}

@Ghi đè
public void onDestroy() {
super.onDestroy();
hidePDialog();
}

private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}



}

我附上了 json 数据的快照。在快照中,我们可以看到我必须访问的颜色“Text=15% discount on bill”。 nhập mô tả hình ảnh ở đây

1 Câu trả lời

thử {
String yourresponseString ="";// this string refer to your api response
JSONObject jsonObject = new JSONObject(yourresponseString);
JSONArray objJsonArray = new JSONArray(jsonObject.getString("restaurants"));
for (int i = 0; i < objJsonArray.length(); i++) {
JSONArray objInnerJsonArray = objJsonArray.getJSONObject(i).getJSONArray("restaurant_offers");
for (int j = 0; j < objInnerJsonArray.length(); j++) {
//Here you can acess youe bill discount value
JSONObject objInnerJSONObject = objInnerJsonArray.getJSONObject(j);
System.out.println("Discount==>" + objInnerJSONObject.getString("text"));
}
}
} bắt (JSONException e) {
e.printStackTrace();
}

你可以像这样解析。使用这个类你可以解析任何类型的层次结构。

关于android - 如何在json中访问对象>数组>对象>数组>对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39077362/

26 4 0
Bài viết được đề xuất: c# - 将时间转换为军事
Bài viết được đề xuất: c# - .NET 中的 Web 请求超时
Bài viết được đề xuất: c# - 使用 .net 学习多线程的好资源?
Bài viết được đề xuất: android - Gradle flatDir 模块路径
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com