Java 仅获取json的第一个元素
试图在真实响应时打开此json,但仅获取此api_数据的第一个元素 无法获取其余数据。 我尝试了下面提到的方法来获取“api_数据”,但无法获取, 无法获取为什么日志中的其余数据对我不可用Java 仅获取json的第一个元素,java,android,json,android-json,Java,Android,Json,Android Json,试图在真实响应时打开此json,但仅获取此api_数据的第一个元素 无法获取其余数据。 我尝试了下面提到的方法来获取“api_数据”,但无法获取, 无法获取为什么日志中的其余数据对我不可用 { "api_req": true, "api_data": [{ "ID": "1", "project_name": "project test xen", "project_content": "lita5645" }, {
{
"api_req": true,
"api_data": [{
"ID": "1",
"project_name": "project test xen",
"project_content": "lita5645"
}, {
"ID": "5",
"project_name": "C\/o 48 No Houses T-II and 48 No houses T-III in New Police Lines Faridabad",
"project_content": "lita5646"
}]
}
我的改装类获取数据。。来自api..
public void onResponse(Call<ResponseActivityList> call, Response<ResponseActivityList> response) {
progressDialog.dismiss();
//progressDialog.setCanceledOnTouchOutside(true);
// Response Success or Fail
if (response.isSuccessful()) {
if (response.body().isApi_req()) {
//success uploading the data
// results.setText(response.body().getResponse());
// Toast.makeText(XenImageUploading.this, "success " + response.body().get_apiData(), Toast.LENGTH_SHORT).show();
Gson gson = new Gson();
data.add(gson.toJson(response.body().getApi_data()));
for (int i = 0 ; i<data.size(); i++){
Log.e("Array", String.valueOf(data.size()));
projectnae_content.add(response.body().getApi_data().get(i).getID());
//Log.e("project",projectnae_content.toString());
//project_id.add(response.body().getApi_data().get(i).getID().toString());
}
// Log.e("project_name" ,projectnae_content.toString());
//Toast.makeText(XenImageUploading.this, jsonInString, Toast.LENGTH_SHORT).show();
//String Project_name_content= response.body().getApi_data();
//Log.e("intersection_list", intersection_list.get(0));
} else {
//error uploading data
//results.setText(response.body().getResponse());
//Toast.makeText(XenImageUploading.this, R.string.string_upload_fail, Toast.LENGTH_SHORT).show();
Log.e("failure", String.valueOf(response.body().isApi_req()));
}
} else {
//error uploading data
//Toast.makeText(XenImageUploading.this, R.string.string_upload_fail, Toast.LENGTH_SHORT).show();
Log.e("failure2", response.message());
}
}
public void onResponse(调用、响应){
progressDialog.disclose();
//progressDialog.setCanceledOnTouchOutside(true);
//响应成功或失败
if(response.issusccessful()){
if(response.body().isApi_req()){
//上传数据成功
//results.setText(response.body().getResponse());
//Toast.makeText(XenImageUploading.this,“success”+response.body().get_apiData(),Toast.LENGTH_SHORT.show();
Gson Gson=新的Gson();
add(gson.toJson(response.body().getApi_data());
对于(inti=0;i您需要在接收内容后解析数据。这里我附上示例演示来解析您的json
try {
JSONObject jsonObject = new JSONObject("hello");
JSONArray api_data=jsonObject.getJSONArray("api_data");
for (int i = 0; i < api_data.length(); i++) {
String id=api_data.getJSONObject(i).getString("ID");
String name=api_data.getJSONObject(i).getString("project_name");
String content=api_data.getJSONObject(i).getString("project_content");
}
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
JSONObject JSONObject=新的JSONObject(“你好”);
JSONArray api_data=jsonObject.getJSONArray(“api_数据”);
对于(int i=0;i
试试这个
JSONObject jsonObj = new JSONObject(Response);
// Getting JSON Array node
JSONArray JsonArray = jsonObj.getJSONArray("api_data");
// looping through All JSON Array
for (int i = 0; i < JsonArray.length(); i++) {
JSONObject data = JsonArray.getJSONObject(i);
Log.e("ID :-> ",data.getString("ID"));// get id
Log.e("project_name :-> ",data.getString("project_name"));// get project name
Log.e("project_content :-> ",data.getString("project_content"));// get project content
}
JSONObject jsonObj=新的JSONObject(响应);
//获取JSON数组节点
JSONArray JSONArray=jsonObj.getJSONArray(“api_数据”);
//循环遍历所有JSON数组
for(int i=0;i”,data.getString(“ID”);//获取ID
Log.e(“项目名称:->”,data.getString(“项目名称”);//获取项目名称
Log.e(“项目内容:->”,data.getString(“项目内容”);//获取项目内容
}
像这样解析,必须将属性与JSON匹配
JSONObject jsonObj = new JSONObject(Response);
JSONArray JsonArray = jsonObj.getJSONArray("api_data");
for (int i = 0; i < JsonArray.length(); i++) {
JSONObject jsonObject = JsonArray.getJSONObject(i);
String id=jsonObject.getString("ID");
String projectname=jsonObject.getString("project_name");
String content=jsonObject.getString("project_content");
}
JSONObject jsonObj=新的JSONObject(响应);
JSONArray JSONArray=jsonObj.getJSONArray(“api_数据”);
for(int i=0;i
你是如何解析数据的?当你很晚才得到json的第一个元素时,你没有展示你是如何解析json的,但是谢谢大家的帮助。。