My Json file is
{"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
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
And it's not working.. As I guess it's something different with normal Json on Internet. 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
Here you can do the following code for obtaining id and target_id values from your json:
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. the array itself contains a number of JSON objects. First, extract a JSON object and an array, then extract JSON objects from the array. My solution for storing these data is based on object-oriented programming. Create a Java object with two variables called DataObject
. An integer for "code"
and a list of another Java object called IdPair
for storing "id"
and "target_id"
. First, define the classes. IdPair object class:
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:
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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.