keysArray
(A,B,C)是dynamic
和cores
具有相同的对象名称?请帮忙!!!
{
"result":"success",
"keysArray":[
"A",
"B",
"C",
"D",
"E"
],
"cores":{
"A":[{"key":"value"}],
"B":[{"key":"value"}],
"C":[{"key":"value"}],
"D":[{"key":"value"}],
"E":[{"key":"value"}]
}
}
答案 0 :(得分:2)
我已经以这种方式实现了它。
String responseStr = //JsonResponse here
Type type = new TypeToken<GetMainResponse>() {}.getType();
GetMainResponse getMainResponse = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create().fromJson(responseStr, type);
int size = getMainResponse.getKeysArray().size();
for (int i = 0; i < size; i++) {
JSONArray jCoreIngredients = new JSONObject(responseStr)
.getJSONArray(getMainResponse.getKeysArray().get(i));
String innerStr = jCoreIngredients.toString();
Type type2 = new TypeToken<ArrayList<GetMainResponse.InnerCores>>() {}.getType();
ArrayList<GetMainResponse.InnerCores> ingredientsInner = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create().fromJson(innerStr, type2);
}
//And this is my POJO
public class GetMainResponse{
String result;
ArrayList<String> keysArray;
Cores cores;
public String getResult() { return result; }
public void setResult(String result) { this.result = result; }
public ArrayList<String> getKeysArray() { return keysArray; }
public void setKeysArray(ArrayList<String> keysArray) { this.keysArray = keysArray; }
public Data getCores() { return data; }
public void setCores(Cores cores) { this.cores = cores; }
public class Cores {
// No declaration and not getter setter here.
public class InnerCores {
String key;
public String getKey() { return key; }
public void setKey(String result) { this.key = key; }
}
}
任何建议:)
答案 1 :(得分:1)
答案 2 :(得分:1)
尝试使用它来获取我们的状态和描述。基于此代码,您可以创建自己的代码。 这是主页的代码。
Gson gson = new Gson();
AboutUsRootObject joc = gson.fromJson(responseOutput.toString(), AboutUsRootObject.class);
resultStatus = joc.status;
resultOutputMsg = joc.message;
然后创建一个新类 AboutUsRootObject.java 并编写此代码:
public class AboutUsRootObject {
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public AboutUsData getData() {
return data;
}
public void setData(AboutUsData data) {
this.data = data;
}
public int status ;
public String message ;
public AboutUsData data ;
}
这里为数据定义了 getter setter 。数据键中包含更多数据,其核心键也是如此在你的中再次创建一个类似于上面的新类getter setter并继续。
答案 3 :(得分:-1)
试试这个:
try {
JSONObject obj1 = new JSONObject(str);
Gson gson = new Gson();
Demo2 demo2 = gson.fromJson(obj1.toString(),Demo2.class);
data3 = gson.toJson(demo2);
Log.e("Main Activity",data3.toString());
}catch (JSONException e) {
e.printStackTrace();
}
和Demo2类:
public class Demo2 {
private String result;
private ArrayList<String> keysArray;
private Core cores;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public ArrayList<String> getKeysArray() {
return keysArray;
}
public void setKeysArray(ArrayList<String> keysArray) {
this.keysArray = keysArray;
}
private class Core {
private ArrayList<ClassD> D;
private ArrayList<ClassE> E;
private ArrayList<ClassC> C;
private ArrayList<ClassB> B;
private ArrayList<ClassA> A;
public ArrayList<ClassA> getA() {
return A;
}
public ArrayList<ClassB> getB() {
return B;
}
public ArrayList<ClassC> getC() {
return C;
}
public ArrayList<ClassD> getD() {
return D;
}
public ArrayList<ClassE> getE() {
return E;
}
private class ClassA {
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
private class ClassB {
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
private class ClassC {
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
private class ClassE {
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
private class ClassD {
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
}
public Core getCores() {
return cores;
}
public void setCores(Core cores) {
this.cores = cores;
}
}