我有这样的JSON,
{
"resultFlag": true,
"successMessage": "Data Received",
"category": [
{
"CategoryID": "4",
"CategoryTitle": "YSafety",
"CategoryImage": "",
"Percentage": "25%",
"Level": 16,
"Days": 60,
"Topic": [
{
"TopicID": "11",
"TopicTitle": "Safety during Online transactions",
"TopicImage": ""
},
{
"TopicID": "12",
"TopicTitle": "Anti-Theft mobile Features",
"TopicImage": ""
},
{
"TopicID": "32",
"TopicTitle": "Magic of Science",
"TopicImage": ""
}
],
"Sponsor": [
{
"SponsorID": 1,
"SponsorName": "adidas",
"SponsorImage": "http://assets/upload/SPONSOR/adidas.jpg"
}
]
},
{
"CategoryID": "5",
"CategoryTitle": "YSoccer",
"CategoryImage": "",
"Percentage": "25%",
"Level": 16,
"Days": 60,
"Topic": [
{
"TopicID": "13",
"TopicTitle": "Others",
"TopicImage": ""
},
{
"TopicID": "14",
"TopicTitle": "Rules",
"TopicImage": ""
},
{
"TopicID": "33",
"TopicTitle": "Magic of Nature",
"TopicImage": ""
}
],
"Sponsor": [
{
"SponsorID": 1,
"SponsorName": "adidas",
"SponsorImage": "http://assets/upload/SPONSOR/adidas.jpg"
}
]
} ]
}
我的代码是获取数组里面的数组,
static ArrayList<String> al_sponsor_image = new ArrayList<String>();
static ArrayList<ArrayList<String>> al_al_topic_id = new ArrayList<ArrayList<String>>();
if (resultFlag == true) {
String success = response.getString("successMessage");
JSONArray array_category = response.getJSONArray("category");
for (int i = 0; i < array_category.length(); i++) {
JSONObject obj_category = array_category.getJSONObject(i);
String category_id = obj_category.getString("CategoryID");
String category_title = obj_category.getString("CategoryTitle");
String category_image = obj_category.getString("CategoryImage");
String percentage = obj_category.getString("Percentage");
String level = obj_category.getString("Level");
String days = obj_category.getString("Days");
// al_category_id.add(category_id);
al_category_title.add(category_title);
// al_category_image.add(category_image);
al_percentage.add(percentage);
al_level.add(level);
al_days.add(days);
JSONArray array_topic = obj_category.getJSONArray("Topic");
JSONArray array_sponsor = obj_category.getJSONArray("Sponsor");
al_topic_id.clear();
al_topic_title.clear();
al_topic_image.clear();
al_sponsor_id.clear();
al_sponsor_name.clear();
al_sponsor_image.clear();
for (int j = 0; j < array_topic.length(); j++) {
JSONObject obj_topic = array_topic.getJSONObject(j);
String topic_id = obj_topic.getString("TopicID");
String topic_title = obj_topic.getString("TopicTitle");
String topic_image = obj_topic.getString("TopicImage");
al_topic_id.add(topic_id);
al_topic_title.add(topic_title);
al_topic_image.add(topic_image);
/* String[] st_sub_array_topic_title = al_topic_title.toArray(new String[al_topic_title.size()]);
String[] st_sub_array_topic_image = al_topic_image.toArray(new String[al_topic_image.size()]);
st_array_topic_title = new String[i][];
st_array_topic_title[j] = st_sub_array_topic_title;
int[] itemImageList = new int[j];
itemImageList[j] = R.drawable.dummy_flower;
st_array_topic_image[j] = itemImageList;*/
/*{itemImageList, itemImageList, itemImageList, itemImageList, itemImageList, itemImageList, itemImageList, itemImageList};*/
/*{R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower, R.drawable.dummy_flower};*/
// st_array_topic_image = new String[i][];
// st_array_topic_image[j] = st_sub_array_topic_image;
}
al_al_topic_id.add(i, al_topic_id);
al_al_topic_title.add(i, al_topic_title);
al_al_topic_image.add(i, al_topic_image);
System.out.println("al_al_topic_id : " + al_al_topic_id);
System.out.println("al_al_topic_title : " + al_al_topic_title);
System.out.println("al_al_topic_image : " + al_al_topic_image);
// String[][] st_array_topic_image
for (int j = 0; j < array_sponsor.length(); j++) {
JSONObject obj_sponsor = array_sponsor.getJSONObject(j);
String sponsor_id = obj_sponsor.getString("SponsorID");
String sponsor_name = obj_sponsor.getString("SponsorName");
String sponsor_image = obj_sponsor.getString("SponsorImage");
al_sponsor_id.add(sponsor_id);
al_sponsor_name.add(sponsor_name);
al_sponsor_image.add(sponsor_image);
/*String[] st_sub_array_sponsor_name = al_sponsor_name.toArray(new String[al_sponsor_name.size()]);
String[] st_sub_array_sponsor_image = al_sponsor_image.toArray(new String[al_sponsor_image.size()]);
st_array_sponsor_name = new String[i][];
st_array_sponsor_name[j] = st_sub_array_sponsor_name;
st_array_sponsor_image = new String[i][];
st_array_sponsor_image[j] = st_sub_array_sponsor_image;*/
}
al_al_sponsor_id.add(i, al_sponsor_id);
al_al_sponsor_name.add(i, al_sponsor_name);
al_al_sponsor_image.add(i, al_sponsor_image);
System.out.println("al_al_sponsor_id : " + al_al_sponsor_id);
System.out.println("al_al_sponsor_name : " + al_al_sponsor_name);
System.out.println("al_al_sponsor_image : " + al_al_sponsor_image);
}
/* String[] st_array_category_title = al_category_title.toArray(new String[al_category_title.size()]);
String[] st_array_percentage = al_percentage.toArray(new String[al_percentage.size()]);
String[] st_array_level = al_level.toArray(new String[al_level.size()]);
String[] st_array_days = al_days.toArray(new String[al_days.size()]); */
rv_learning_cards.setAdapter(new AdapterLearningCards(activity, al_category_title, al_percentage, al_level, al_days, al_al_topic_image, al_al_topic_title, al_al_sponsor_image));
}
当我打印al_al_topic_title时,它用lat数组替换所有数组......
al_al_topic_title : [[Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities], [Travel and Activity Gear, Types of Activities]]
al_al_topic_title : [[Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others], [Others, Others]]
任何人都可以给我建议吗?
答案 0 :(得分:0)
解决问题的基本方法是错误的,您可以使用具有
的bean类Category.java轻松完成此操作public class Category
{
String CategoryID;
String CategoryTitle;
String CategoryImage;
String Percentage;
String Level;
String Days;
ArrayList<Topic> topic;
.......
Getter and setter methods here
.........
}
class Topic
{
String TopicID;
String TopicTitle;
String TopicImage;
.......
Getter and setter methods here
.........
}
现在将已解析的数据添加到ArrayList<Category> category
。然后上面的问题也不会出现,其他开发人员也会更清楚地理解代码。
答案 1 :(得分:0)
您的代码可能有错误,您可以通过这种方式在json中找到您想要的任何内容:
<强> 1 强>
转到http://www.jsonschema2pojo.org/网站并粘贴您的json (这个站点为你的json(Pojo)生成java类)
<强> 2 强>
将java文件复制到项目中
第3 强>
您应该使用序列化/反序列化库将json字符串转换为java对象 我推荐你Gson
<强> 4 强>
将Gson导入您的项目后,请编写此代码
Gson gson = new Gson();
YOUR_GENERETED CLASS response = gson.fromJson(YOUR_JSON_STRING ,YOUR_GENERETED CLASS.class);
最后你可以从响应对象中获取你想要的东西
答案 2 :(得分:0)
使用Gson解析您的JSON响应,而不是手动解析它。它会减少很多问题。
Gson是一个开源库,用于将Java对象序列化和反序列化为(和来自)JSON。
您需要为JSON响应创建普通的java对象类,如下所示。
CategoryJsonResponse.java
class CategoryJsonResponse {
boolean resultFlag;
String successMessage;
ArrayList<Category> category;}
Category.java
class Category {
int CategoryID;
String CategoryTitle;
String CategoryImage;
String Percentage;
int Level;
int Days;
ArrayList<Topic> Topic;
ArrayList<Sponsor> Sponsor;}
Sponser.java
class Sponsor {
int SponsorID;
String SponsorName;
String SponsorImage;}
Topic.java
class Topic {
int TopicID;
String TopicTitle;
String TopicImage;}
现在,您需要编写以下几行代码来解析您的JSON响应。我假设我们正在&#34;响应&#34;变量
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
CategoryJsonResponse categoryJsonResponse = gson.fromJson(response.toString(), CategoryJsonResponse .class);
现在,您在categoryJsonResponse对象中有完整的JSON响应,您可以用普通的java方式读取任何值。最后一点。要在项目中添加GSON,您只需在应用程序级别gradle文件中添加以下依赖项。
compile 'com.google.code.gson:gson:2.2.4'