How to send multiple array with array of objects send to android - java

I am using this code this format shows Invalid JSON Please tell me any code of android
Format:-->
{
"supplyType":"O",
"subSupplyType":"1",
"subSupplyDesc":"TESTDESCRIPTION",
"docType": "BIL",
"docNo": "18-86"
"itemList": [{
"productName": "Wheat",
"productDesc": "Wheat",
"hsnCode": 84,
"quantity": 10,
"qtyUnit": "BOX",
"cgstRate": 0,
"sgstRate": 0,
"igstRate": 12,
"cessRate": 0,
"cessAdvol": 0,
"taxableAmount":5609889
},
{
"productName": "Wheat",
"productDesc": "Wheat",
"hsnCode": 84,
"quantity": 10,
"qtyUnit": "BOX",
"cgstRate": 0,
"sgstRate": 0,
"igstRate": 12,
"cessRate": 0,
"cessAdvol": 0,
"taxableAmount":5609889
}]
}

Check this -
{
"supplyType":"O",
"subSupplyType":"1",
"subSupplyDesc":"TESTDESCRIPTION",
"docType": "BIL",
"docNo": "18-86",
"itemList": [
{
"productName": "Wheat",
"productDesc": "Wheat",
"hsnCode": 84,
"quantity": 10,
"qtyUnit": "BOX",
"cgstRate": 0,
"sgstRate": 0,
"igstRate": 12,
"cessRate": 0,
"cessAdvol": 0,
"taxableAmount":5609889
},
{
"productName": "Wheat",
"productDesc": "Wheat",
"hsnCode": 84,
"quantity": 10,
"qtyUnit": "BOX",
"cgstRate": 0,
"sgstRate": 0,
"igstRate": 12,
"cessRate": 0,
"cessAdvol": 0,
"taxableAmount":5609889
}
]
}

Related

How to properly read local json file and store the data from it in variables in Android studio?

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?

JSONArray throws A JSONArray text must start with '[' at 1 [character 2 line 1] exception

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 ','

PACT for a JSON array of integers

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
}

How to implement for loop for JsonArray Data [duplicate]

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

Convert json to java object with gson

As part of my computer science IA I am creating a tool that reads match history and details of dota games and generates stats and hero stats. To do this I have accessed the valve API and grabbed a few jsons of matches and match history from it, then cut them down slightly so they only contain the information I need in the json.
Below is a sample of the details of one of the matches in a json format:
"result": {
"players": [
{
"account_id": 40884464,
"player_slot": 0,
"hero_id": 31,
"kills": 8,
"deaths": 8,
"assists": 14,
"last_hits": 72,
"denies": 0,
"gold_per_min": 304,
"xp_per_min": 412,
"level": 18,
},
{
"account_id": 70638797,
"player_slot": 1,
"hero_id": 35,
"kills": 6,
"deaths": 7,
"assists": 4,
"last_hits": 212,
"denies": 37,
"gold_per_min": 371,
"xp_per_min": 356,
"level": 17,
},
{
"account_id": 76281087,
"player_slot": 2,
"hero_id": 5,
"kills": 3,
"deaths": 13,
"assists": 10,
"last_hits": 22,
"denies": 0,
"gold_per_min": 215,
"xp_per_min": 259,
"level": 14,
},
{
"account_id": 4294967295,
"player_slot": 3,
"hero_id": 28,
"kills": 11,
"deaths": 11,
"assists": 11,
"last_hits": 166,
"denies": 18,
"gold_per_min": 413,
"xp_per_min": 485,
"level": 20,
},
{
"account_id": 81692493,
"player_slot": 4,
"hero_id": 2,
"kills": 1,
"deaths": 9,
"assists": 7,
"last_hits": 135,
"denies": 8,
"gold_per_min": 261,
"xp_per_min": 314,
"level": 16,
},
{
"account_id": 10101141,
"player_slot": 128,
"hero_id": 30,
"kills": 7,
"deaths": 8,
"assists": 25,
"last_hits": 90,
"denies": 2,
"gold_per_min": 382,
"xp_per_min": 421,
"level": 18,
},
{
"account_id": 62101519,
"player_slot": 129,
"hero_id": 7,
"kills": 6,
"deaths": 8,
"assists": 20,
"last_hits": 305,
"denies": 0,
"gold_per_min": 556,
"xp_per_min": 585,
"level": 22,
},
{
"account_id": 134700328,
"player_slot": 130,
"hero_id": 4,
"kills": 17,
"deaths": 2,
"assists": 13,
"last_hits": 335,
"denies": 16,
"gold_per_min": 729,
"xp_per_min": 724,
"level": 25,
},
{
"account_id": 35357393,
"player_slot": 131,
"hero_id": 83,
"kills": 4,
"deaths": 4,
"assists": 23,
"last_hits": 16,
"denies": 4,
"gold_per_min": 318,
"xp_per_min": 407,
"level": 18,
},
{
"account_id": 4294967295,
"player_slot": 132,
"hero_id": 101,
"kills": 13,
"deaths": 8,
"assists": 12,
"last_hits": 57,
"denies": 3,
"gold_per_min": 390,
"xp_per_min": 405,
"level": 18,
}
]
,
"radiant_win": false,
"duration": 2682,
"start_time": 1461781997,
"match_id": 2324299045,
"match_seq_num": 2036251155,
"cluster": 133,
"game_mode": 1,
"flags": 0,
"engine": 1,
"radiant_score": 30,
"dire_score": 48
}
Using an intelliJ plugin I have created 3 Java classes, one with the match result, one for the details of the result, and one for the details of the players within the result, each with the variables gets sets in:
TestMatch fields:
private TestMatchResult result;
TestMatchResult fields:
private int duration;
private int start_time;
private int cluster;
private boolean radiant_win;
private int match_seq_num;
private int engine;
private TestMatchResultPlayers[] players;
private long match_id;
private int dire_score;
private int flags;
private int game_mode;
private int radiant_score;
TestMatchResultPlayers fields:
private int kills;
private int gold_per_min;
private int last_hits;
private int account_id;
private int assists;
private int level;
private int player_slot;
private int xp_per_min;
private int hero_id;
private int denies;
private int deaths;
I have downloaded and added the gson library as a dependency into the intelliJ project.
I am trying to parse the json into the java classes as an object and would like to do that for all the match jsons, however I am not quite sure how to do that at the moment, all I have is:
public static void getMatch()
{
Gson gson = new Gson();
}
Could someone who understands gson better than myself give me a little bit of guidance as to how I'd go about parsing that json into the class(es) as an object for several match jsons? Once I've done that the rest of what I need to do is easy since it's just a case of taking the variables and running calculations on them then displaying them. If it's not possible or practical I can make a test CSV and read from that instead as I know how to use them, but only just come across jsons as that is what the valve API returns requests in so figured I may as well learn how to use them.
Thanks!
you need to use the method Gson.fromJson()
Example:
public static void getMatch()
{
Gson gson = new Gson();
TestMatch tm = gson.fromJson(jsonString, TestMatch.class);
}

Categories