[英]Gson with complicated Json Android Studio
My Json file is 我的Json文件是
{"code":0,"datas":[{"id":0,"target_id":0},{"id":1,"target_id":0
}................]}
And what I wrote is.. // String data; 我写的是.. //字符串数据; // data has the Json as String //数据将Json作为字符串
JsonParser jsonParser = new JsonParser();
JsonObject object = (JsonObject) jsonParser.parse(data);
JsonArray memberArray = (JsonArray) object.get("datas");
JsonObject object1= (JsonObject) memberArray.get(0);
dataParsed = object1.get("id").getAsString();
// wanted print 1 //想要打印1
And it's not working.. As I guess it's something different with normal Json on Internet. 而且它不起作用。.我想这与Internet上的普通Json有所不同。 I thought "code" : 0 is the problem I want to know how to seperate this json code / data and get id and target id String 我以为“代码”:0是我想知道如何分隔此json代码/数据并获取ID和目标ID字符串的问题
Here you can do the following code for obtaining id and target_id values from your json: 在这里,您可以执行以下代码以从json获取id和target_id值:
JsonParser jsonParser = new JsonParser();
JsonObject object = (JsonObject) jsonParser.parse(new String(Files.readAllBytes(Paths.get("test.json"))));
JsonArray memberArray = (JsonArray) object.get("datas");
for (JsonElement jsonElement : memberArray) {
System.out.println(
String.format("Id : %d TId: %d",
jsonElement.getAsJsonObject().get("id").getAsInt(),
jsonElement.getAsJsonObject().get("target_id").getAsInt()));
}
Output is: 输出为:
Id : 0 TId: 0
Id : 1 TId: 0
Your JSON
contains an integer ("code") plus an array. 您的JSON
包含一个整数(“代码”)和一个数组。 the array itself contains a number of JSON objects. 数组本身包含许多JSON对象。 First, extract a JSON object and an array, then extract JSON objects from the array. 首先,提取一个JSON对象和一个数组,然后从该数组中提取JSON对象。 My solution for storing these data is based on object-oriented programming. 我存储这些数据的解决方案基于面向对象的编程。 Create a Java object with two variables called DataObject
. 用两个名为DataObject
变量创建一个Java对象。 An integer for "code"
and a list of another Java object called IdPair
for storing "id"
and "target_id"
. "code"
的整数,以及另一个用于存储"id"
和"target_id"
Java对象IdPair
的列表。 First, define the classes. 首先,定义类。 IdPair object class: IdPair对象类:
public class IdPair {
int id;
int target_id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getTarget_id() {
return target_id;
}
public void setTarget_id(int target_id) {
this.target_id = target_id;
}
}
DataObject class: DataObject类:
public class DataObject {
private int code;
private List<IdPair> idPairs;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public List<IdPair> getIdPairs() {
return idPairs;
}
public void setIdPairs(List<IdPair> idPairs) {
this.idPairs = idPairs;
}
}
then start extracting data from your json: 然后开始从json提取数据:
DataObject dataObject = new dataObject(); // init object
JSONObject jsonObject = new JSONObject(data); // whole json is an object! (data is your json)
int code = jsonObject.optInt("code"); // get code from main json
dataObject.setCode(code); // set code to our object
List<IdPair> pairs = new ArrayList<>(); // init list of id pairs
JSONArray datas = jsonObject.optJSONArray("datas"); // get json array from main json
IdPair pair = null; // define idPairs object
for (int i=0; i<datas.length() ; i++){ // jasonArray loop
JSONObject object = new JSONObject(datas(i)); // each jsonObject in the jsonArray
pair = new IdPair();// init idPair
int id = object.optInt("id"); // get the object id
int targetId = object.optInt("target_id"); // get the object target_id
// set values to our object
pair.setId(id);
pair.setTarget_id(targetId);
//add object to list
pairs.add(pair);
}
// your dataObject is now filled with JSON data
Note: this is a general solution. 注意:这是一个常规解决方案。 For example, you could use a HashMap
instead of IdPair
object. 例如,您可以使用HashMap
代替IdPair
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.