json values in toast type在我的项目中从服务器端获取数据,因为我正在发送带有嵌套数组的数组作为响应。在此图中,您可以看到带有数组的“ Secondary_number”具有一些值。我需要打印这些值。 我附上了我的java代码。当我尝试运行此屏幕时,我的屏幕变黑。
JSONObject obj = new JSONObject(response);
JSONArray jsonArray = obj.getJSONArray("response");
service = new String[jsonArray.length()];
//Iterate the jsonArray and print the info of JSONObjects
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i)
//getting second array
JSONArray jArray = jsonObject.getJSONArray("secondary_number");
//Iterate the jsonArray and print the info of JSONObjects
for (int j = 0; j < jArray.length(); j++) {
do some computation
}
service[i] = jsonObject.getString("service_id").toString(); //getting values in response
tv1 = jsonObject.getString("service_id");
tv2 = jsonObject.getString("user_id").toString();
tv3 = jsonObject.getString("username").toString();
tv4 = jsonObject.getString("company_name").toString();
tv5 = jsonObject.getString("assigned_time").toString();
请为此提供帮助
答案 0 :(得分:0)
前两行都可以
JSONObject obj = new JSONObject(response);
JSONArray services = obj.getJSONArray("response");
但是在那之后,不是。我不知道您想做什么,可能是出于调试目的,所以我不会做任何事情。另外,我将得到字符串以使其清楚。
for(int i = 0; i < services.length(); i++){
JSONObject service = services.getJSONObject(i);
tv1 = service.getString("service_id");
tv2 = service.getString("assigned_time");
tv3 = service.getString("company_name");
tv4 = service.getString("user_id");
tv5 = service.getString("username");
//for some reason, secondary_number value is escaped which mean
//it is a string that you will have to parse
String secondary_number = service.getString("secondary_number");
JSONArray secondary_number_json = new JSONArray(secondary_number);
for(int j = 0; j < secondary_number_json.length(); j++){
JSONObject secondary = secondary_number_json.getJSONObject(j);
String name = secondary .getString("name");
String alternative_no = secondary .getString("alternative_no");
String designation = secondary .getString("designation");
// do things with those strings
}
}
为了更好地利用数据,您应该查看Gson库,以便使用pojo类更轻松地解析json