I have the following JSON data:
JSON:
[{
"valInternalCode": "NE",
"valMinimumInputString": "NE",
"valExternalRepresentation": "Northeast",
"valActionCode1": "1",
"valActionCode2": null,
"valActionCode3": null,
"valActionCode4": null,
"id": {
"valcodeId": "X.LOCATION",
"pos": 1
},
"uniqueId": "X.LOCATION-1",
"caption": "Northeast"
}, {
"valInternalCode": "NW",
"valMinimumInputString": "NW",
"valExternalRepresentation": "Northwest",
"valActionCode1": "1",
"valActionCode2": null,
"valActionCode3": null,
"valActionCode4": null,
"id": {
"valcodeId": "X.LOCATION",
"pos": 2
},
"uniqueId": "X.LOCATION-2",
"caption": "Northwest"
}
I am able to parse it and create a Map from the data like so:
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<Map>>(){}.getType();
Collection<Map> dataCollection = gson.fromJson(jsonString.toString(), collectionType);
I can then iterate through it and get a value that I need using the Key like so:
iterator.next().get("valInternalCode");
What I am struggling with is how to get something that is inside of the id field:
"id": {
"valcodeId": "X.LOCATION",
"pos": 2
},
I am using Hibernate to get the data from a non normalized Oracle database (that is why hibernate creates the id field the way it does)
EDIT:
My attempt at an ugly way of doing it. Basically looping within the loop:
while (valIterator.hasNext()) {
Map currentVal = valIterator.next();
String valId = "";
Collection<Map> idVal = (Collection<Map>) currentVal.get("id");
Iterator<Map> valIdIterator = idVal.iterator();
while (valIdIterator.hasNext()) {
Map valIdCurrentVal = valIdIterator.next();
valId = valIdCurrentVal.get("valcodeId").toString();
}
}
Getting a ClassCastException when I try to cast currentVal.get("id") to a Collection
Related
How can I compare two JSON, one I get from the database and the second One from FrontEnd(means when I click on the edit button)? How can I compare these two JSON?
First Json
{
"id": 2,
"createdAt": "07-01-2021 15:26:16",
"updatedAt": "07-01-2021 15:26:16",
"offerAcceptedDate": "06-30-2021 07:14:00",
"offerAmount": 100000,
"offerDate": "06-30-2021 07:14:00",
"offerFile": "string",
"possibleCloseDate": "06-30-2021 07:14:00",
"remarks": "string",
"salesId": 8,
"status": "Active",
"contactId": 32,
"createdById": 1,
"offerAcceptedById": 1
}
2nd JSON
{
"createdAt": "09-01-2021 15:26:16",
"updatedAt": "10-01-2021 15:26:16",
"offerAcceptedDate": "06-30-2021 07:14:00",
"offerAmount": 500000,
"offerDate": "06-30-2021 07:14:00",
"offerFile": "string",
"possibleCloseDate": "06-30-2021 07:14:00",
"remarks": "string",
"salesId": 8,
"status": "Active",
"contactId": 32,
"createdById": 1,
"offerAcceptedById": 1
}
Also, compare these two JSON and also show activity what column" changed.
Like this:
createdAt: Changed
updatedAt: Changed
offeramount: Changed
1)First of all I get data from DB using findById(JPA Repository)
2)Compare Db field and request body field
like that :
LeadOffer leadOffer = leadOfferRepository.findById(offerId).orElse(null);
JsonObject jsonObject = new JsonObject();
JsonArray jsonArray= new JsonArray();
if (leadOffer.getAmount() != leadOfferRequest.getAmount())
{
offer.setAmount(leadOfferRequest.getAmount());
jsonObject.addProperty("amount",leadOfferRequest.getAmount() );
jsonObject1.addProperty("amount",leadOffer.getAmount() );
}
jsonArray.add(jsonObject);
You can use guava library for this purpose. For example :
String json1 = "{\"name\":\"ABC\", \"city\":\"XYZ\", \"state\":\"CA\"}";
String json2 = "{\"city\":\"XYZ\", \"street\":\"123 anyplace\", \"name\":\"ABC\"}";
Gson g = new Gson();
Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> firstMap = g.fromJson(json1, mapType);
Map<String, Object> secondMap = g.fromJson(json2, mapType);
System.out.println(Maps.difference(firstMap, secondMap).entriesDiffering());
This shows the keys that are present in both the maps but have different values
i have the following json as a get response:
{
"global": {
"name": "Lz Cha0S",
"uid": xxx,
"platform": "X1",
"level": 521,
"toNextLevelPercent": 31,
"internalUpdateCount": 22822,
"bans": {
"isActive": false,
"remainingSeconds": 0,
"last_banReason": "COMPETITIVE_DODGE_COOLDOWN"
},
"rank": {
"rankScore": 4227,
"rankName": "Gold",
"rankDiv": 2,
"rankImg": "https:\/\/api.apexlegendsstatus.com\/assets\/ranks\/gold2.png"
},
"battlepass": {
"level": "-1"
}
},
"realtime": {
"lobbyState": "open",
"isOnline": 0,
"isInGame": 0,
"canJoin": 0,
"partyFull": 0,
"selectedLegend": "Bloodhound"
},
"legends": {
"selected": {
"LegendName": "Bloodhound",
"data": [
{
"name": "Kills",
"value": 331,
"key": "kills"
},
{
"name": "Beast of the hunt kills",
"value": 62,
"key": "beast_of_the_hunt_kills"
},
{
"name": "Season 4 Wins",
"value": 20,
"key": "wins_season_4"
}
],
"ImgAssets": {
"icon": "http:\/\/api.apexlegendsstatus.com\/assets\/icons\/bloodhound.png",
"banner": "http:\/\/api.apexlegendsstatus.com\/assets\/banners\/bloodhound.jpg"
}
},
"all": {cutted becouse off to much Text...},
"mozambiquehere_internal": {
"isNewToDB": false,
"claimedBy": "-1",
"APIAccessType": "BASIC",
"ClusterID": "2",
"rate_limit": {
"max_per_second": 3,
"current_req": "1"
}
},
"total": {
"kills": {
"name": "Kills",
"value": 331
},
"beast_of_the_hunt_kills": {
"name": "Beast of the hunt kills",
"value": 62
},
"wins_season_4": {
"name": "Season 4 Wins",
"value": 20
},
"kd": {
"value": -1,
"name": "KD"
}
}
}
I saved this json to a map with the skecthware block
Json [response] to Map [response]json to map block
Now a want to get the name key with is in the global key, but if i use the block
Map [response] get key [name]
Map get key name
It gives a error that name cannot be found (null exeption)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at com.chaos droid.attack.MainActivity$3.onResponse(MainActivity.java:365) se(MainActivity.java:365) at com.chaosdroid.attack.RequestNetwork er$3$2.run(RequestNetworkController.java:171) at android.os.Handler.handleCallback(Handler.java:
873) at
android.os.Handler.dispatchMessage(Handler.ja va:99) at android.os.Looper.loop(Looper.java:193) at android.app.Activity Thread.main(ActivityThread.j ava:6718) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491) ArgsCaller.run(RuntimeInit.java:491)
at com.android.internal.os.ZygoteInit.main(Zygotel nit.java:858)
END APPLICATION
If i use the global key in the get key block it returns the global key as Text
Map [response] get key [global]
get map key global
returns
{name=Lz Cha0S, uid=xxx, platform=X1, level=521.0, toNextLevelPercent=31.0, internalUpdateCount=22822.0, bans={isActive=false, remainingSeconds=0.0, last_banReason=COMPETITIVE_DODGE_COOLDOWN}, rank={rankScore=4227.0, rankName=Gold, rankDiv=2.0, rankImg=https://api.apexlegendsstatus.com/assets/ranks/gold2.png}, battlepass={level=-1}}
i tried to save that output to a New map variable but its not a valid json objet so the json to map block is not working.
It looks like the map value can store only one key with value not a child key. When i get the response where i have an error in the get field it returns an error json that looks like this:
{"Error": "Player not found. Try again?"}
Here can i get the key succsessful with the same get key block:
Map [response] get key [global]
Get map.key error
i also tried to parse the json with Java in an add source directly block with:
JSONObject jObject = new JSONObject(result);
String aJsonString = jObject.getString("name");
But then i get a sketchware Compiler error that jsonobjet cannot be resolved to a type
1. ERROR in /storage/emulated/0/.sketchware/mysc/717/app/src/main/ java/com/chaosdroid/atrack/MainActivity.java (at line 355) String result = response;
Type mismatch: cannot convert from HashMap<String,Object> to String
2. ERROR in /storage/emulated/0/.sketchware/mysc/717/app/src/main/ java/com/chaos droid/attack/MainActivity.java (at line 357) JSONObject jObject = new JSONObject(result);
JSONObject cannot be resolved to a type
3. ERROR in /storage/emulated/0/.sketchware/mysc/717/app/src/main/ java/com/chaosdroid/atrack/MainActivity.java (at line 357) JSONObject Object = new JSONObject(result);
JSONObject cannot be resolved to a type
3 problems (3 errors)
And i dont know how to import jsonobject into sketchware.
How can a get the child key of global from this json?
Edit: (thanks to #Dmytro Kyrychkov)
For sketchware users: you need the full declaration of the function you want to use because you cant import
libraries in sketchware. For this example this will working in an add source directly block in sketchware:
String jsonStr = strResponse;
Gson gson = new Gson();
HashMap<String, Object> json = gson.fromJson(jsonStr, HashMap.class);
com.google.gson.internal.LinkedTreeMap<String, Object> global = (com.google.gson.internal.LinkedTreeMap<String, Object>) json.get("global");
String name = (String) global.get("name");
If you want to get a JSON object child field you should follow its structure during parsing:
public static void main(String... args) throws IOException {
String jsonStr = new String(Files.readAllBytes(PATH_TO_JSON));
Gson gson = new Gson();
HashMap<String, Object> json = gson.fromJson(jsonStr, HashMap.class);
LinkedTreeMap<String, Object> global = (LinkedTreeMap<String, Object>) json.get("global");
String name = (String) global.get("name");
System.out.println(name);
}
Output:
Lz Cha0S
Process finished with exit code 0
I hope it helps you :)
Create two map variables, map1 and map2
now get your response in map1
and then use Json[map:[map1]get kay "global"] to map:[map2]],
Now you have your JSON data ready to use with ListMap, remember that your JSON data is now inside map2, so whenever you want to use it remember to get it from map2.
I am working on velocty and java, I was to take values from an json file (which I took using recursion of JSON OBjects and storing path of recursion(See context below to get idea)
String hhh="I am a ${root.primaryApplicant.firstName} ${firstName} ";
Velocity.init();
VelocityContext vm=new VelocityContext();
for(String m : mp.keySet())
{
vm.put(m,mp.get(m));
}
StringWriter w = new StringWriter();
Velocity.evaluate( vm, w, "mystring", forma );
The map mp is obtained from a json file
{
"id": 45288,
"code": null,
"name": null,
"external": false,
"leadReferenceId": "APPID1573716175142",
"createdBy": null,
"createdDate": "2017-10-26T12:14:17.000Z",
"agentName": null,
"ipAddress": null,
"applicationType": "HOME",
"loanType": "HOME",
"applicantType": {
"id": 269,
"code": "Single",
"name": "Single",
"external": false
},
"relationship": null,
"primaryApplicant": {
"id": 45289,
"code": null,
"name": null,
"external": false,
"existingCustomer": null,
"customerId": null,
"partyRoleType": {
"id": 348,
"code": "0",
"name": "PRIMARY",
"external": false
},
"partyRelationshipType": null,
"salutation": {
"id": 289,
"code": "MR",
"name": "Mr",
"external": false
},
"firstName": "Anuj",
"middleName": "",
"lastName": "singh",
"dateOfBirth": "1986-12-11T18:30:00.000Z",
"genderType": {
using a debugger the context of vm contains
"root.primaryApplicant.firstName" -> "Anuj"
"firstName" -> "Anuj"
after Velocity evaluate I get
"I am a ${root.primaryApplicant.firstName} Anuj ";
i am assuming it cant replace keys with . in between.
Is there any better way to populate the string
----------------
The velocity file has a "root.*" specified and since I cant edit those, I am using the following recursion program to get the keys
private static void rexuse(String a,Map<String, Object> mp,JSONObject js,String parent) throws JSONException {
Iterator<String> keys = js.keys();
while(keys.hasNext()) {
String key = keys.next();
if(key.equals("name"))
{
mp.put(parent,js.get(key));
}
if (js.get(key) instanceof JSONObject) {
rexuse(a+"."+key,mp,js.getJSONObject(key),key);
}
else
{
if(!mp.containsKey(key) ||( mp.containsKey(key) && mp.get(key)==null))
mp.put(key, js.get(key));
mp.put(a+"."+key, js.get(key) );
}
}
}
where a is the prefix and called the above using
String a="root";
rexuse(a,mp,js,"root");
There is an inconsistency, here.
With the Java initialization code you give, the Velocity template should contain:
${primaryApplicant.firstName}
If you want to use ${root.primaryApplicant.firstName}, then the other reference should also be prefixed by root, as in ${root.firstName}, and the Java context initialization code should be:
vm.put("root", mp);
But in both cases you must also check that the json library you are using provides a Json object with a generic getter, so that the 'dot' operator will recursively call the Java method get(<fieldname>) on the json object. There are tons of those.
I've been having some problems iterating through a JSON object in Java.
Specifically, I'd like to save each value for "name" to the string array "nameList". I've looked up how to do this, and I haven't found a way for this situation.
String[] nameList = new String[]{};
{
"data": {
"Narray": {
"0":
{
"_id": "001",
"name": "studio",
"date": "02141992"
},
"1":
{
"_id": "002",
"name": "venue",
"date": "09041999"
}
}
}
Ideally you'd want Narray to be an actual JSON array, enclosed in [], with each element being another object, containing the property you need, like this:
{
"Narray": [
{
"_id": "001",
"name": "studio",
"date": "02141992"
},
{
"_id": "002",
"name": "venue",
"date": "09041999"
}
]
}
Then you can use jackson to decode the JSON string into a POJO structure. Once you have the objects, you can iterate over the array and retrieve the property you need into a list.
I assume that you only want to save each value of name into a String[] (string array), so you don't need to deserialize the JSON string to POJO, just use basic API to achieve what you want as follows:
BTW, your JSON string is invalid, you miss a right bracket.
ObjectMapper mapper = new ObjectMapper();
JsonNode nArray = mapper.readTree(jsonStr).get("data").get("Narray");
String[] nameList = new String[nArray.size()];
for (int i = 0; i < nArray.size(); i++) {
nameList[i] = nArray.get(String.valueOf(i)).get("name").asText();
}
System.out.println(Arrays.toString(nameList));
Console output:
[studio, venue]
In my android project, I have the string text which got the following data:
[
{
"admin": true,
"created_at": "2012-10-16T07:26:49Z",
"email": "asdf#gmail.com",
"id": 28,
"language": "fr",
"name": "Marc",
"profile_pic_content_type": null,
"profile_pic_file_name": null,
"profile_pic_file_size": null,
"profile_pic_updated_at": null,
"provider": null
},
{
"admin": false,
"created_at": "2013-04-02T18:47:36Z",
"email": "asdf2#gmail.com",
"id": 263,
"language": "en",
"name": "Marcus",
"profile_pic_content_type": null,
"profile_pic_file_name": null,
"profile_pic_file_size": null,
"profile_pic_updated_at": null,
"provider": null
}
]
I converted it into a json object thanks to this:
JSONObject jsonObj = new JSONObject(text);
I want to parse that Json object, and setting it inside a ListView, but even with the official documentation I can't succeed in doing so.
After parsing, I want to keep only the first part of the array, and delete every field excepting the email, language and name, to get this in the end:
[
{
"email": "asdf#gmail.com",
"language": "fr",
"name": "Marc"
}
]
You're dealing with a JSONArray - the [ ] - that contains two separate JSONObject. The way you extract values from this structure is simply to go piece by piece, first getting the nested objects from the array and then extracting their internal values. You can then repackage it as you wish. For example:
int numObject = jsonArray.length();
JSONArray repackArray = new JSONArray();
for(int i = 0; i < numObject; i++){
JSONObject nested = jsonArray.getJsonObject(i);
//get values you need
String email = nested.getString("email");
String language = nested.getString("language");
String name = nested.getString("name");
//add values to new object
JSONObject repack = new JSONObject();
repack.put("email", email);
repack.put("language", language);
repack.name("name", name);
//add to new array
repackArray.put(repack);
}
Alternatively if put doesn't work for you, you can always create your own String in JSON format and then simply create a new JSONObject using that string as an argument in the constructor. I assumed you were working with a JSONArray in the above example. If you're starting with a JSONObject the process is the same. Just get the JSONArray out of the object first before unpacking.