How to read this json with gson? - java

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
}
......
'

Related

Spring boot Controller json response has a field name "empty"

I have a GET endpoint in my application supposed to return :
{
"gameId": "41a483c4-6220-424a-a931-d9114a4f6748",
"pits": [
{
"id": 1,
"stones": 6
},
{
"id": 2,
"stones": 6
},
{
"id": 3,
"stones": 6
},
{
"id": 4,
"stones": 6
},
{
"id": 5,
"stones": 6
},
{
"id": 6,
"stones": 6
},
{
"id": 7,
"stones": 0
},
{
"id": 8,
"stones": 6
},
{
"id": 9,
"stones": 6
},
{
"id": 10,
"stones": 6
},
{
"id": 11,
"stones": 6
},
{
"id": 12,
"stones": 6
},
{
"id": 13,
"stones": 6
},
{
"id": 14,
"stones": 0
}
],
"playerTurn": null,
"currentPitIndex": 0
}
but instead it returns:
{
"id": "25f09303-b797-418f-a7e7-db0e5fa8631b",
"pits": [
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 0,
"empty": true
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 6,
"empty": false
},
{
"stones": 0,
"empty": true
}
],
"playerTurn": null,
"currentPitIndex": null
}
I am wondering what is "empty"?! and where is "id"!
would be much appreciated for any suggestion and help.
thank you
Does your class have a method called isEmpty()? Because most JSON marshalling frameworks add JSON properties for any method with 0 arguments that starts with get, or returns a boolean and starts with is. For the same reason id is probably missing - I'm guessing there is no getter for it. The top level gameId is probably called id because you have a getId() method, not a getGameId() method.
For most frameworks you can tweak this with annotations. For instance, with Jackon you can use #JsonIgnore to indicate a method should not be represented as a JSON property, and #JsonProperty can be used to a) add a custom name, or b) allow a field to be included as well. For other frameworks you should check their documentation.

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

Create a json from a List of list Java

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.

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
}

Categories