This question already has answers here:
How to parse JSON in Java
(36 answers)
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 4 years ago.
Hi i am not able to do looping for the below multidimensional JsonArray Data which is coming from nodejs Server.
Even the index values of the jsonarray will also increase dynamically whenever more data will come into it. Please help me to implement for loop to get data dynamically.
Response which is i am getting from server is:-
[
[{
"id": 3,
"user_id": 22,
"coin": "btc",
"coin_quantity": 4.24524129,
"order": 1,
"order_price": 175,
"total_amount": 742.92,
"order_type": 0,
"processed": 3.85931026,
"remaining": 0.02425852,
"status": 1,
"t_fee_inr": 125.92,
"t_fee_coin": 0,
"t_gst": 22.66,
"invoice": null,
"create_time": "2018-03-20T21:22:49.000Z",
"complete_time": null
}, {
"id": 5,
"user_id": 22,
"coin": "btc",
"coin_quantity": 2.24524129,
"order": 1,
"order_price": 174.8,
"total_amount": 392.47,
"order_type": 0,
"processed": 0,
"remaining": 2.24524129,
"status": 0,
"t_fee_inr": 0,
"t_fee_coin": 0,
"t_gst": 0,
"invoice": null,
"create_time": "2018-03-21T19:41:19.000Z",
"complete_time": null
}, {
"id": 7,
"user_id": 22,
"coin": "btc",
"coin_quantity": 0.64524129,
"order": 1,
"order_price": 174.85,
"total_amount": 112.82,
"order_type": 0,
"processed": 0,
"remaining": 0.64524129,
"status": 0,
"t_fee_inr": 0,
"t_fee_coin": 0,
"t_gst": 0,
"invoice": null,
"create_time": "2018-03-21T19:42:08.000Z",
"complete_time": null
}, {
"id": 9,
"user_id": 22,
"coin": "btc",
"coin_quantity": 0.76324129,
"order": 1,
"order_price": 174.89,
"total_amount": 133.48,
"order_type": 0,
"processed": 0,
"remaining": 0.76324129,
"status": 0,
"t_fee_inr": 0,
"t_fee_coin": 0,
"t_gst": 0,
"invoice": null,
"create_time": "2018-03-21T19:43:07.000Z",
"complete_time": null
}],
[{
"total_buy_orders": 4
}], {
"page_no": "1"
}
]
You can try this way
JSONArray array = yourJsonArrayFromServer;
for(int i = 0 ; i < array.length(); i++){
// get current object with: array.getJSONObject(i)
// do something
}
If I understand well your question.
var test = []; //your array
for(let key of test){
if(key instanceof Array) {
console.log('array'); //detect if its an array then do iteration
getValues(key);
}else{
console.log(key); //value to access, its an object
}
}
function getValues(key){
for(let val of key){
console.log(val); //value to access, its an object
}
}
This will work for you using JQuery .each() function
$.each(json, function (key, data) {
$.each(data, function (index, data) {
console.log('index', data)
})
});
json is your JsonArray Data
Related
I have a JSON file, which I've stored in the assets folder locally.
The content of the file has two JSON arrays, cats and services , stored in a JSON object:
{
"result": 0,
"msg": "No error",
"cats": [
{
"catid": 1,
"catnameEN": "Mobile Network Operators"
},
{
"catid": 2,
"catnameEN": "SIP and Landline"
},
{
"catid": 3,
"catnameEN": "ISP and VoIP Providers"
},
{
"catid": 23,
"catnameEN": "Utility Bills"
},
],
"services": [
{
"srvid": 98,
"catid": 1,
"paymin": ".1000",
"paymax": "2000.0000",
"fixedprice": 0,
"servicenameEN": "Super Mobile",
},
{
"srvid": 90,
"catid": 1,
"paymin": ".0500",
"paymax": "2000.0000",
"fixedprice": 0,
"servicenameEN": "International Operator",
},
{
"srvid": 91,
"catid": 1,
"paymin": ".5000",
"paymax": "2000.0000",
"fixedprice": 0,
"servicenameEN": "Z-MOBILE",
},
{
"srvid": 296,
"catid": 2,
"paymin": ".5000",
"paymax": "1000.0000",
"fixedprice": 0,
"servicenameEN": "Telecom Payphone",
},
{
"srvid": 51,
"catid": 3,
"paymin": ".5000",
"paymax": "1000.0000",
"fixedprice": 0,
"servicenameEN": "Fast Internet",
},
}
These two arrays share the same catid, the purpose of it is to link two SQL tables with ID. So when a category with id = 1 is pressed, I will query to return all services that has id = 1.
My thought is to store these two arrays (cats and services) in some type of data structure (Array?) in Java separately and only store catid, catnameEN, servicenameEN data, and then loop through in order to one by one execute SQL insert operation.
How should I read this local file? How should I store the data?
The following JSON Object (a variable named json_object) is composed of two values
peripheral_devices
peripheral_tests
both of which are arrays:
{
"peripheral_devices": [
{
"_id": 1,
"active": -1,
"battery": "63",
"bt_firmware_version": "btv1.7.777",
"configured": 0,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323224,
"device_id": 72,
"discovered": 0,
"firmware_version": "v3.14",
"hardware_version": "null",
"icon": "icon_device_bp5",
"last_connect_date": 1640023710420,
"mac_address": "8C:DE:52:41:FC:57",
"model": "BP5 41FC57",
"name": "BP5",
"other_id": "-1",
"paired": -1,
"type_id": 1,
"update_date": 1635807323224,
"user_id": 13
},
{
"_id": 3,
"active": -1,
"battery": "90",
"bt_firmware_version": "1.0.0",
"configured": -1,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323239,
"device_id": 72,
"discovered": 0,
"firmware_version": "1.0.0",
"hardware_version": "5.0.0",
"icon": "icon_device_hs2s",
"last_connect_date": 1640022147928,
"mac_address": "00:4D:32:0C:B7:2C",
"model": "HS2S 11070",
"name": "HS2S",
"other_id": "-1",
"paired": -1,
"type_id": 3,
"update_date": 1635807323239,
"user_id": 13
}
],
"peripheral_tests": [
{
"_id": 199,
"consumer_id": 22,
"create_date": 1640020949760,
"end_date": 1640020949078,
"mood_id": -1,
"notes": "",
"start_date": 1640020939533,
"status_id": 1,
"type_id": 2,
"update_date": 0,
"user_id": -99
},
{
"_id": 198,
"consumer_id": 22,
"create_date": 1640020904183,
"end_date": -1,
"metric_bps": {,
"mood_id": -1,
"notes": "",
"start_date": 1640020863742,
"status_id": 1,
"type_id": 1,
"update_date": 0,
"user_id": -99
},
{
"_id": 197,
"consumer_id": 22,
"create_date": 1640020834664,
"end_date": 1640020828741,
"mood_id": -1,
"notes": "",
"start_date": 1640020822580,
"status_id": 1,
"type_id": 3,
"update_date": 0,
"user_id": -99
}
]
}
and derived as follows:
JSONObject json_object = new JSONObject(post_data);
Using org.json.JSONArray.JSONArray:
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_devices");
works as expected and a json array is parsed from json_object. However, using the same technique to get the second array in the object does not:
JSONArray json_tests = new JSONArray("peripheral_tests");
throws exception:
A JSONArray text must start with '[' at 1 [character 2 line 1]
Why? What am I not understanding?
You code
JSONArray json_tests = new JSONArray("peripheral_tests");
Take string in the constructor which is the JSON data and peripheral_tests is not JSON array, to get peripheral_tests you need to get it from json_object like
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_tests");
The input JSON is invalid, accoring to JSON Lint ...which may lead to unexpected results:
Error: Parse error on line 69:
..."metric_bps": { , "mood_id": -1,
----------------------^
Expecting 'STRING', '}', got ','
I have a list of lists that I want to convert to json, the list looks like:
[Country, Food, Detail, Population, Region, Extension]
[Germany, 1, 2, 3, 4, 5]
[England, 10, 11, 12, 13, 14]
[USA, 19, 20, 21, 22, 23]
[China, 28, 29, 30, 31, 32]
[Italy, 37, 38, 39, 40, 41]
[France, 46, 47, 48, 49, 50]
I want to create a json like this
"Germany"{
"Food":
{
"value": 1
},
"Detail":
{
"value": 2
},
"Population":
{
"value": 3
},
"Region":
{
"value": 4
},
"Extension":
{
"value": 5
}
}
"Italy"{
"Food":
{
"value": 10
},
"Detail":
{
"value": 11
},
"Population":
{
"value": 12
},
"Region":
{
"value": 13
},
"Extension":
{
"value": 14
}
}...
I was trying with gson but I have can not created it, I have a problem when I add the other json into the last json. I get a result like this:
"Germany"{
"Food":
{
"value": 1
}
...}
"Germany"{
"Food":
{
"value": 10
}
"Detail":
{
"value": 11
}
...}
"Italy"{
"Food":
{
"value": 10
}
"Detail": {
"value": 11
}
...}
This is the part of my code where I create the json
JsonObject main2= new JsonObject();
JsonObject main = new JsonObject();
for (int row = 1; row < table.size(); row++) {
for (int colum = 1; colum < table.get(0).size(); colum++) {
JsonObject jsObj = new JsonObject();
jsObj.add("value", new JsonParser().parse(table.get(row).get(colum)));
main.add(table.get(0).get(colum), jsObj);
}
main2.add(table.get(row).get(0),main);
gson.toJson(main2, writer);
}
I am not sure what are I doing wrong, If I write the "main" the result is correct but when I write the "main2" I have that problem. There are other option because I tried in deferents ways buy I don have a correct result :(
In the last line of inner for loop, it should be main2 instead of main.
Got the following JSON:
{
"content": [
7,
8,
9,
10
],
"last": true,
"total_elements": 9,
"total_pages": 2,
"first": false,
"number_of_elements": 4,
"size": 5,
"number": 1,
"empty": false
}
and I would like to create a contract with PACT using the following code:
DslPart body = newJsonBody((root) -> {
root.array("consumer", a -> a.integerType().integerType().integerType())
.booleanType("last")
.numberType("total_elements")
.numberType("total_pages")
.booleanType("first")
.numberType("number_of_elements")
.numberType("size")
.numberType("number")
.booleanType("empty");
}).build();
Although this is working, I really do not like the array contract using a concatenation of integerType.
My question is: is there a better way to indicate an array of X elements, which have to be of type Integer?
You can use minArrayLike.
#Test
public void test() {
DslPart body = newJsonBody((root) ->
root.minArrayLike("content", 4, PactDslJsonRootValue.integerType(1), 4)
.booleanType("last")
.numberType("total_elements")
.numberType("total_pages")
.booleanType("first")
.numberType("number_of_elements")
.numberType("size")
.numberType("number")
.booleanType("empty"))
.build();
System.out.println(body.toString());
}
Produces
{
"content": [
1,
1,
1,
1
],
"number": 100,
"last": true,
"size": 100,
"total_elements": 100,
"total_pages": 100,
"number_of_elements": 100,
"first": true,
"empty": true
}
I have this Json and I don't know how to read when the atributtes are dynamic...
Note that example below, the field "atletas" is a List by "Ids"... How can I get this information if I don't know what Id will get?
For example:
{
"rodada": 2,
"atletas":
"100651": {
"apelido": "Rodrygo",
"pontuacao": 1.3,
"scout": {
"FC": 3,
"FF": 1,
"FS": 1,
"RB": 1
},
"foto": "https://s.glbimg.com/es/sde/f/2017/11/01/b8128d3d5db5325dc238cadc67c28342_FORMATO.png",
"posicao_id": 5,
"clube_id": 277
},
"101957": {
"apelido": "Thiago Larghi",
"pontuacao": 0,
"scout": {
},
"foto": "https://s.glbimg.com/es/sde/f/2018/03/19/b1b3b42713071408fb76d25cfb76d927_FORMATO.jpeg",
"posicao_id": 6,
"clube_id": 282
},
"36773": {
"apelido": "Julio Cesar",
"pontuacao": 8,
"scout": {
"DD": 1,
"SG": 1
},
"foto": "https://s.glbimg.com/es/sde/f/2018/04/17/326a0f6b72076eb12b4e6b335ae6a1da_FORMATO.png",
"posicao_id": 1,
"clube_id": 262
}
......
'