How to traverse through inner Json array and retrieve the values - java

I have an response Json given below I need to retrieve the values of price from inner "childItems" Array and sum it up to get the total price = 7.7
Below is the java code i just tried with which I guess is bit wrong.
List<Float> items_price = response.jsonPath().getList("items.childItems.price");
float Total_Price=0;
for(int i=0;i<items_price.size();i++)
{
Total_Price = Total_Price + items_price.get(i);
}
Total_Price = Total_Price + response.jsonPath().getFloat("tax");
Below is the Response Json
==========================
{
"brandId": "HAL",
"sellingChannel": "WEBOA",
"subTotal": 7.19,
"total": 7.7,
"items": [
{
"lineItemId": 101,
"description": "Apple Buffet",
"price": 0,
"priceType": null,
"productId": "HAL-itm-000-057",
"childItems": [
{
"lineItemId": 102,
"description": "Apple",
"price": 3.31,
"priceType": null,
"productId": "HAL-itm-000-055",
"childItems": [],
"modifierGroups": [
{
"description": null,
"productId": "HAL-prg-001-001",
"modifiers": [
{
"productId": "HAL-itm-006-011",
"price": 2,
"priceType": null,
"actionCode": null,
"quantity": 1
}
]
},
{
"description": null,
"productId": "HAL-prg-001-002",
"modifiers": []
},
{
"description": null,
"productId": "HAL-prg-001-003",
"modifiers": [
{
"productId": "HAL-itm-008-011",
"price": 0.29,
"priceType": null,
"actionCode": null,
"quantity": 1
},
{
"productId": "HAL-itm-008-004",
"price": 0,
"priceType": null,
"actionCode": null,
"quantity": 1
},
{
"productId": "HAL-itm-008-005",
"price": 0.29,
"priceType": null,
"actionCode": null,
"quantity": 1
}
]
},
{
"description": null,
"productId": "HAL-prg-001-004",
"modifiers": [
{
"productId": "HAL-itm-009-013",
"price": 0,
"priceType": null,
"actionCode": null,
"quantity": 1
}
]
},
{
"description": null,
"productId": "HAL-prg-001-005",
"modifiers": []
},
{
"description": null,
"productId": "HAL-prg-001-006",
"modifiers": []
},
{
"description": null,
"productId": "HAL-prg-001-008",
"modifiers": [
{
"productId": "HAL-itm-013-007",
"price": 0,
"priceType": null,
"actionCode": null,
"quantity": 1
}
]
},
{
"description": null,
"productId": "HAL-prg-001-010",
"modifiers": [
{
"productId": "HAL-itm-015-001",
"price": 0,
"priceType": null,
"actionCode": null,
"quantity": 1
}
]
}
],
"quantity": 1,
"note": null
},
{
"lineItemId": 103,
"description": "Vegetable Fires",
"price": 1.99,
"priceType": null,
"productId": "HAL-itm-002-002",
"childItems": [],
"modifierGroups": [
{
"description": null,
"productId": "HAL-prg-001-006",
"modifiers": []
},
{
"description": null,
"productId": "HAL-prg-001-005",
"modifiers": []
}
],
"quantity": 1,
"note": null
},
{
"lineItemId": 104,
"description": "Coca-Cola®",
"price": 1.89,
"priceType": null,
"productId": "HAL-itm-003-001",
"childItems": [],
"modifierGroups": [
{
"description": null,
"productId": "HAL-prg-001-011",
"modifiers": [
{
"productId": "HAL-itm-016-001",
"price": 0,
"priceType": null,
"actionCode": null,
"quantity": 1
}
]
},
{
"description": null,
"productId": "HAL-prg-001-012",
"modifiers": [
{
"productId": "HAL-itm-017-001",
"price": 0,
"priceType": null,
"actionCode": null,
"quantity": 1
}
]
}
],
"quantity": 1,
"note": null
}
],
"modifierGroups": [],
"quantity": 1,
"note": null
}
],
"fulfillment": {
"fulfillmentType": "TimePickup",
"asap": true,
"pickupTime": null,
"storeLocation": {
"locationId": "99983"
}
},
"tallyTime": "2018-03-25T12:41:29.949Z",
"tax": 3.51
}
Please let me know your thoughts on this. Thanks in advance.

Just tried the below approach and it worked for me.
JSONObject results = new JSONObject(response.asString());
JSONArray childItems= results.getJSONArray("items").getJSONObject(0).getJSONArray("childItems");
float Total_Price =0;
for(int i=0;i<childItems.length();i++)
{
//scenario.write(childItems.toString());
scenario.write(+i +" : "+childItems.getJSONObject(i).getFloat("price"));
Total_Price = Total_Price + childItems.getJSONObject(i).getFloat("price");
}
Total_Price = Total_Price + response.jsonPath().getFloat("tax");
Assert.assertEquals(Total_Price, response.jsonPath().getFloat("total"),"Total Price in response is not equal to sum of line items price plus tax");

Related

Java Json Repeating the key

I have a problem always i call the method addplayers he inserts into the jsonfile the array required, but, he always create new key , so, i'm trying to insert a new player in the key Players always i create a new player.
I created some conditions for that but is not working, like parsing the file and check the key.
the expecteded result:
{
"players": [
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
},
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
},
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
}
]
}
the result at moment:
{
"players": [
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
},
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
},
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
}
]
}
{
"players": [
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
},
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
},
{
"current_Energy": 100,
"level": 1,
"name": "tenente",
"experiencePoints": 0,
"team": "Giants"
}
]
}
The Method
public void addPlayer(PlayerManager playerManager) {
if (file.exists()) {
System.out.println("FODASEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
try {
PrintStream printStream = new PrintStream(new FileOutputStream("data.json",true));
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray;
ArrayUnorderedList<Players> dataplayer = playerManager.getPlayersList();
try {
jsonObject = (JSONObject) new JSONParser().parse(new FileReader(file));
jsonArray = (JSONArray) jsonObject.get("players");
} catch (ParseException e) {
// Caso não exista, cria um novo objeto JSON com a chave "players"
jsonArray = new JSONArray();
}
Set<String> keys = new HashSet<>();
if (jsonObject.containsKey("players")) {
keys.add("players");
}
if (!keys.contains("players")) {
jsonArray = new JSONArray();
jsonObject.put("players", jsonArray);
} else {
jsonArray = (JSONArray) jsonObject.get("players");
}
for (Players player : dataplayer) {
JSONObject playerObject = new JSONObject();
playerObject.put("name", player.getName());
playerObject.put("team", player.getTeam().toString());
playerObject.put("level", player.getLevel());
playerObject.put("experiencePoints", player.getExperiencePoints());
playerObject.put("current_Energy", player.getCurrentEnergy());
jsonArray.add(playerObject);
}
printStream.println(jsonObject);
printStream.flush();
printStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
At a glance the problem seems to be that you're reading and changing the same file, which you have open in append mode:
PrintStream printStream = new PrintStream(new FileOutputStream("data.json",true));
The true parameter there means whatever you write on that file will be appended at the end when you write to it, which seems compatible with the behaviour you're seeing.
Depending on what you need to achieve, you might prefer to just write on a different file as output.

change name of property in a list to list nested json using JOLT

I am news to jolt json transformation. So looking for help in transforming nested json having list of values to nested json with list as following.
Input json
{
"userData": [
{
"userId": "1",
"age": "20",
"firstName": "firstname1",
"lastname": "lastname1",
"zipCode": "zipcode1",
"street": "street1",
"city": "city1",
"country": "country",
"gender": "gender1",
"grade": "grade1",
"birthday": "birthday1"
},
{
"userId": "2",
"age": "25",
"firstName": "firstname2",
"lastname": "lastname2",
"zipCode": "zipcode2",
"street": "street2",
"city": "city2",
"country": "country2",
"gender": "gender2",
"grade": "grade2",
"birthday": "birthday2"
}
]
}
Jolt Specification
[
{
"operation": "shift",
"spec": {
"userData": {
"*": {
"userId": "data.[&1].ID",
"*": "data.[&1].&",
"zipCode": {
"#custom-field1": "adr_&1.property",
"#": "adr_&.value"
},
"street": {
"#custom-field2": "adr_&1.property",
"#": "adr_&.value"
},
"city": {
"#custom-field3": "adr_&1.property",
"#": "adr_&.value"
},
"country": {
"#custom-field4": "adr_&1.property",
"#": "adr_&.value"
}
}
}
}
}
]
Output
{
"data" : [ {
"ID" : "1",
"age" : "20",
"firstName" : "firstname1",
"lastname" : "lastname1",
"gender" : "gender1",
"grade" : "grade1",
"birthday" : "birthday1"
}, {
"ID" : "2",
"age" : "25",
"firstName" : "firstname2",
"lastname" : "lastname2",
"gender" : "gender2",
"grade" : "grade2",
"birthday" : "birthday2"
} ],
"adr_zipCode" : {
"property" : [ "custom-field1", "custom-field1" ],
"value" : [ "zipcode1", "zipcode2" ]
},
"adr_street" : {
"property" : [ "custom-field2", "custom-field2" ],
"value" : [ "street1", "street2" ]
},
"adr_city" : {
"property" : [ "custom-field3", "custom-field3" ],
"value" : [ "city1", "city2" ]
},
"adr_country" : {
"property" : [ "custom-field4", "custom-field4" ],
"value" : [ "country", "country2" ]
}
}
Expected json
{
"data": [
{
"ID": "1",
"age": "20",
"firstName": "firstname1",
"lastname": "lastname1",
"gender": "gender1",
"grade": "grade1",
"birthday": "birthday1",
"address": [
{
"property": "custom-field1",
"value": "zipcode1"
},
{
"property": "custom-field2",
"value": "street1"
},
{
"property": "custom-field3",
"value": "city1"
},
{
"property": "custom-field4",
"value": "country"
}
]
},
{
"ID": "2",
"age": "25",
"firstName": "firstname2",
"lastname": "lastname2",
"gender": "gender2",
"grade": "grade2",
"birthday": "birthday2",
"address": [
{
"property": "custom-field1",
"value": "zipcode2"
},
{
"property": "custom-field2",
"value": "street2"
},
{
"property": "custom-field3",
"value": "city2"
},
{
"property": "custom-field4",
"value": "country2"
}
]
}
]
}
You can use individual value arrays of their indexes to be used within the upcoming specs with a tricky zeroth member which will be removed later, after been used such as
[
{
"operation": "shift",
"spec": {
"userData": {
"*": {
"userId": "data[&1].ID",
"#(0,age)": "data[&1].age", //multiplexed this attribute in order to make it the zeroth index, which will be removed within the upcoming specs, for the "address" array at the same time, as indexing starts from zero ...
"*": "data[&1].&",
"a*|z*|s*|c*": {//separating "zipCode", "street", "city", "country" attributes from the others while using "a*" for "age" is tricky, temporarily
"#": "data[&2].value"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"data": {
"*": {
"*": "&2[&1].&",
"value": {
"*": {
"#property": "&4[&3].address[&1].custom-field&1",
"#(2,value[&])": "&4[&3].address[&1].value"
}
}
}
}
}
},
{ // the attributes are written individually to keep the desired sorting
"operation": "shift",
"spec": {
"data": {
"*": {
"ID": "&2[&1].&",
"age": "&2[&1].&",
"firstName": "&2[&1].&",
"lastname": "&2[&1].&",
"gender": "&2[&1].&",
"grade": "&2[&1].&",
"birthday": "&2[&1].&",
"address": {
"0": "&4[&3].&2[&1]",
"*": {
"*": {
"$": "&5[&4].&3[&2].#(0)" // key-value pairs are exchanged for this attribute
},
"value": "&4[&3].&2[&1].&"
}
}
}
}
}
},
{ // get rid of null components of the array
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
}
]
[
{
"operation": "shift",
"spec": {
"userData": {
"*": {
"userId": "data.[&1].ID",
"*": "data.[&1].&",
"zipCode": {
"#custom-field1": "data[#3].address[#2].property",
"#": "data[#3].address[#2].value"
},
"street": {
"#custom-field2": "data[#3].address[#2].property",
"#": "data[#3].address[#2].value"
},
"city": {
"#custom-field3": "data[#3].address[#2].property",
"#": "data[#3].address[#2].value"
},
"country": {
"#custom-field4": "data[#3].address[#2].property",
"#": "data[#3].address[#2].value"
}
}
}
}
},
{ // get rid of null components of the array
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
}
]
You may consider another library Josson to do the same job by shorter transformation statement.
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"{" +
" \"userData\": [" +
" {" +
" \"userId\": \"1\"," +
" \"age\": \"20\"," +
" \"firstName\": \"firstname1\"," +
" \"lastname\": \"lastname1\"," +
" \"zipCode\": \"zipcode1\"," +
" \"street\": \"street1\"," +
" \"city\": \"city1\"," +
" \"country\": \"country\"," +
" \"gender\": \"gender1\"," +
" \"grade\": \"grade1\"," +
" \"birthday\": \"birthday1\"" +
" }," +
" {" +
" \"userId\": \"2\"," +
" \"age\": \"25\"," +
" \"firstName\": \"firstname2\"," +
" \"lastname\": \"lastname2\"," +
" \"zipCode\": \"zipcode2\"," +
" \"street\": \"street2\"," +
" \"city\": \"city2\"," +
" \"country\": \"country2\"," +
" \"gender\": \"gender2\"," +
" \"grade\": \"grade2\"," +
" \"birthday\": \"birthday2\"" +
" }" +
" ]" +
"}");
Transformation
JsonNode node = josson.getNode(
"map(data:userData" +
" .field(ID:userId, userId:," +
" zipCode:, street:, city:, country:," +
" address:collect(" +
" map(property:'customer-field1',value:zipCode)," +
" map(property:'customer-field2',value:street)," +
" map(property:'customer-field3',value:city)," +
" map(property:'customer-field4',value:country))))");
System.out.print(node.toPrettyString());
Output
{
"data" : [ {
"age" : "20",
"firstName" : "firstname1",
"lastname" : "lastname1",
"gender" : "gender1",
"grade" : "grade1",
"birthday" : "birthday1",
"ID" : "1",
"address" : [ {
"property" : "customer-field1",
"value" : "zipcode1"
}, {
"property" : "customer-field2",
"value" : "street1"
}, {
"property" : "customer-field3",
"value" : "city1"
}, {
"property" : "customer-field4",
"value" : "country"
} ]
}, {
"age" : "25",
"firstName" : "firstname2",
"lastname" : "lastname2",
"gender" : "gender2",
"grade" : "grade2",
"birthday" : "birthday2",
"ID" : "2",
"address" : [ {
"property" : "customer-field1",
"value" : "zipcode2"
}, {
"property" : "customer-field2",
"value" : "street2"
}, {
"property" : "customer-field3",
"value" : "city2"
}, {
"property" : "customer-field4",
"value" : "country2"
} ]
} ]
}

i want to show value arraylist in arraylist javaspring, how to fix it?

My code
public List<PaymentDetailInfo> PaymentDetailsInfo(List<Transaction> transactionInfo) {
List<PaymentDetailInfo> result = new ArrayList<>();
PaymentDetailInfo pay = new PaymentDetailInfo();
List<PaymentDetailProduct> detProd = new ArrayList<>();
PaymentDetailProduct det = new PaymentDetailProduct();
for (int i = 0; i < transactionInfo.size(); i++) {
pay.setTransactionId(transactionInfo.get(i).getTransactionId());
pay.setYourPayment(transactionInfo.get(i).getYourPayment() == null ? 0 : transactionInfo.get(i).getYourPayment());
pay.setSisaPembayaran(transactionInfo.get(i).getSisaPembayaran() == null ? 0 : transactionInfo.get(i).getSisaPembayaran());
pay.setDendaKeterlambatan(transactionInfo.get(i).getDendaKeterlambatan() == null ? 0 : transactionInfo.get(i).getDendaKeterlambatan());
pay.setJatuhTempo(transactionInfo.get(i).getJatuhTempo() == null ? "" : transactionInfo.get(i).getJatuhTempo());
pay.setTotalPembayaran(transactionInfo.get(i).getTotalPembayaran() == null ? 0 : transactionInfo.get(i).getTotalPembayaran());
for(int j = 0; j<transactionInfo.get(i).getMerchantProduct().size(); j++){
det.setProductId(transactionInfo.get(i).getMerchantProduct().iterator().next().getMerchantProductId());
det.setProductName(transactionInfo.get(i).getMerchantProduct().iterator().next().getProductName());
det.setProductPrice(transactionInfo.get(i).getMerchantProduct().iterator().next().getProductPrice());
det.setRegisteredDate(transactionInfo.get(i).getMerchantProduct().iterator().next().getCreatedDate());
det.setLastModified(transactionInfo.get(i).getMerchantProduct().iterator().next().getUpdatedDate());
detProd.add(det);
pay.setPaymentProduct(detProd);
}
pay.setRegisteredDate(DateUtils.formatDateTimeOrEmptyString(transactionInfo.get(i).getCreatedDate()));
pay.setLastModified(DateUtils.formatDateTimeOrEmptyString(transactionInfo.get(i).getUpdatedDate()));
result.add(pay);
}
return result;
}
{
"meta": {
"code": "2000",
"message": "Sukses"
},
"data": {
"detailPayment": {
"fin_payment": {
"financialName": "BCAFINANCE",
"loanLimit": 10000000,
"sisaLimit": 7900000,
"loanId": "61929149-59f4-4f2e-9ffd-7bbc6370fdaf",
"registeredDate": "06/01/2021 22:20",
"lastModified": "07/Jan/2021 03:03"
},
"det_payment": [
{
"transactionId": "09699913-e006-4684-b1e3-a346991609bd",
"yourPayment": 0,
"sisaPembayaran": 0,
"dendaKeterlambatan": 0,
"jatuhTempo": "",
"paymentProduct": [
{
"productId": "39c3cfdb-5e76-45c3-b0a1-d96d0181a73c",
"productName": "BAJU",
"productPrice": 350000,
"registeredDate": "2021-01-06T22:50:41.270955"
},
{
"productId": "39c3cfdb-5e76-45c3-b0a1-d96d0181a73c",
"productName": "BAJU",
"productPrice": 350000,
"registeredDate": "2021-01-06T22:50:41.270955"
},
{
"productId": "39c3cfdb-5e76-45c3-b0a1-d96d0181a73c",
"productName": "BAJU",
"productPrice": 350000,
"registeredDate": "2021-01-06T22:50:41.270955"
},
{
"productId": "39c3cfdb-5e76-45c3-b0a1-d96d0181a73c",
"productName": "BAJU",
"productPrice": 350000,
"registeredDate": "2021-01-06T22:50:41.270955"
},
{
"productId": "39c3cfdb-5e76-45c3-b0a1-d96d0181a73c",
"productName": "BAJU",
"productPrice": 350000,
"registeredDate": "2021-01-06T22:50:41.270955"
}
],
"totalPembayaran": 0,
"registeredDate": "",
"lastModified": ""
},
{
"transactionId": "09699913-e006-4684-b1e3-a346991609bd",
"yourPayment": 0,
"sisaPembayaran": 0,
"dendaKeterlambatan": 0,
"j

Merge elements of array based on index position or id

I have a list array which I'm getting from the database. I want to create a new List with all elements in the array grouped by their index position or id.
This is the array I'm working with:
[
{
"content": "Content A1",
"id": 1
},
{
"content": "Content A2",
"id": 2
},
{
"content": "Content A3",
"id": 3
},
{
"content": "Content A4",
"id": 4
},
{
"content": "Content A5",
"id": 5
}
]
[
{
"content": "Content B1",
"id": 1
},
{
"content": "Content B2",
"id": 2
},
{
"content": "Content B3",
"id": 3
},
{
"content": "Content B4",
"id": 4
},
{
"content": "Content B5",
"id": 5
}
]
[
{
"content": "Content C1",
"id": 1
},
{
"content": "Content C2",
"id": 2
},
{
"content": "Content C3",
"id": 3
},
{
"content": "Content C4",
"id": 4
},
{
"content": "Content C5",
"id": 5
}
]
This is how I want the new List array to look like:
[
{
"content": {"Content A1","Content B1","Content C1"}
"id": 1
},
{
"content": {"Content A2", "Content B2", "Content C2"}
"id": 2
},
{
"content": {"Content A3","Content B3","Content C3"}
"id": 3
},
{
"content": {"Content A4","Content B4","Content C4"}
"id": 4
},
{
"content": {"Content A5","Content B5","Content C5"}
"id": 5
}
]
I have tried this code but it returns only the last object:
List<String> contentArray = new ArrayList<>();
for (int i = 0; i < b.listDetail.size(); i++) {
int finalI = i;
contentArray = listResource.stream()
.map(a -> a.listDetail.get(finalI).content)
.collect(Collectors.toList());
}
System.out.println(contentArray);
You are almost done, just need to store contentArray in a list for all index.
List<List<String>> res = new ArrayList<>();
for (int i = 0; i < b.listDetail.size(); i++) {
int finalI = i;
List<String> contentArray = listResource.stream()
.map(a -> a.listDetail.get(finalI).content)
.collect(Collectors.toList());
res.add(contentArray); // store all contents for a index in list
}
You can create a class for your response with id and contents and construct inside loop and add in the list.

Access single value of Jsonarray [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 7 years ago.
I used the code to get a Jsonarray
JSONArray preferenceobj = (JSONArray) personobj.get("preferences");
The a json array like this code bellow. How can I get individual value of ratings.
[{
"rating": 3,
"documentId": "TRECCS-00674898-160",
"tags": ["Romantic", "Seafood", "Family Friendly"]
}, {
"rating": 2,
"documentId": "TRECCS-00247656-160",
"tags": ["Bar-hopping"]
}, {
"rating": 3,
"documentId": "TRECCS-00085961-160",
"tags": ["Gourmet Food"]
}, {
"rating": 4,
"documentId": "TRECCS-00086637-160",
"tags": ["Family Friendly", "Local Food", "Entertainment"]
}, {
"rating": 4,
"documentId": "TRECCS-00086308-160",
"tags": ["Family Friendly", "Tourism"]
}, {
"rating": 4,
"documentId": "TRECCS-00086622-160",
"tags": ["Healthy Food", "Romantic", "Gourmet Food"]
}, {
"rating": 2,
"documentId": "TRECCS-00809111-160",
"tags": ["Wellness", "Family Friendly", "Sport"]
}, {
"rating": 4,
"documentId": "TRECCS-00086310-160",
"tags": ["Family Friendly", "Sport"]
}, {
"rating": 4,
"documentId": "TRECCS-00340169-160",
"tags": ["Fashion Bargains", "Live Music", "Shopping for accessories", "Family Friendly"]
}, {
"rating": 4,
"documentId": "TRECCS-00018110-160",
"tags": ["Healthy Food", "Family Friendly", "Local Food", "Organic Food"]
}, {
"rating": 2,
"documentId": "TRECCS-00085880-160",
"tags": ["Romantic", "Sailing", "Seafood"]
}, {
"rating": 4,
"documentId": "TRECCS-00259825-152",
"tags": ["Family Friendly", "Budget Friendly"]
}]
Use this:
JSONArray preferenceobj = (JSONArray) personobj.get("preferences");
List<Integer> ratings = new ArrayList<>();
for(int i = 0; i < preferenceobj.length(); i++) {
JSONObject obj = preferenceobj.getJSONObject(i);
int rating = obj.getInt("rating"); // Your rating
ratings.add(rating);
}
// Show ratings
for(Integer rating : ratings) {
System.out.println(rating);
}
The answer I solved using the prvious comment of Srarta is:
JSONArray preferenceobj = (JSONArray) personobj.get("preferences");
List<Long> ratings = new ArrayList<>();
for(int i = 0; i < preferenceobj.size(); i++) {
JSONObject obj1 = (JSONObject) preferenceobj.get(i);
Long rating = (Long)obj1.get("rating");
ratings.add(rating);
}
// Show ratings
for(Long rating : ratings) {
System.out.println(rating);
}

Categories