To create JsonArray - java

I want to create a json structure as following:
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 732-1234",
"646 123-4567"
]
}
Using add property method I am able to create firstName and lastName but haven't figured out how to create address and phoneNumbers in Java.
Please help me out in this;
Ajay

What you'll need to do is create a new Class called Address which has those properties (string streetAdress, city, state, postalcode). The structure itself will then have an instance of that class.
Also I wouldn't make postalCode an int. Leave it as a string.
PhoneNumbers will be a list of strings (unless you want to do something fancy with it).

Related

How to get a value from json object?

{
"address": [{
"addressLine1": "Noida",
"addressLine2": "UP"
}],
}
{
"address": [{
"addressLine1": "Noida",
"addressLine2": "UP"
}],
}
{
"address": [{
"addressLine1": "Noida",
"addressLine2": "UP"
}],
}
I have few different json objects in a json file.
I have a java code to read each adresses addressline 1.
I want to know is there a way to skip the second address and go to 3rd one to read addressline?
if you have fixed address list you can do it like address[0].addressLine1,address[0].addressLine2 and address[2].addressLine1,address[2].addressLine2
if u have fixed structure address list you can get the value like this :
alert ( objectJSON.address[0].addressLine1);
alert ( objectJSON.address[0].addressLine2);
alert ( objectJSON.address[2].addressLine1);
alert ( objectJSON.address[2].addressLine2);
please try

How to get value from json object with java obect mapper

I want to get the value at the field first inside name.
How i can access in this field using HashMap in java
{ "payload":{
"name": {
"first": "jean",
"last": "bob,
},
"address": {
"code": "75",
"city": "paris",
"country": "France"
},
}}
Use one of the available Java libraries for handling JSON. E.g. Gson from Guava API. They are pretty straing fw.

Parsing JSON when nested or otherwise a bit more complicated than single values [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 5 years ago.
Here's the JSON:
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "123 Main street",
"city": "Anytown",
"postalCode": "12345"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "123-456-8888"
},
{
"type": "home",
"number": "123-557-8910"
}
]
}
The question is, in Java, how do you access the address fields? I tried things like address.city, but that didn't work:
String city = (String) jsonObject.get("address.streetAddress");
System.out.println(city);
Would appreciate any suggestions.
You need to call getString on the address object that you get.
String city = jsonObject.getJSONObject("address").getString("streetAddress");
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "123 Main street",
"city": "Anytown",
"postalCode": "12345"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "123-456-8888"
},
{
"type": "home",
"number": "123-557-8910"
}
]
}
first get your address.Address is an object.{} means object,[] means an array.
JSONObject addressObj = jsonObject.get("address");
now you have a reference to the address.
String addressStr = addressObj.get("streetAddress");
String cityStr = addressObj.get("city");
int cityInt = Integer.parseInt(addressObj.get("postalCode"));
If i don't wrong remember there should be getString("streetAddress") and getInteger("postalCode") to avoid parse issues.
Apart from the given answers you should add a check to see if the address is not blank before checking city or else you will end up getting and exception.
Here is updated example:
if( jsonObject.has("address"){
JSONObject addressObj = jsonObject.get("address");
if( addressObj.has("city"){
String cityStr = addressObj.get("city");
}
}

Qualified names for JSON objects

Since the json objects are limited in usage of characters I'm looking for a better description for this. The json file will be used later as a table and therefore the objects are the head and the values are the content of the table.
{
"employees": {
"employee": [
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise"
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova"
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond"
}
]
}
}
Therefore I'm looking to use something like a header where I can define the names. These names shall replace the object strings.
e.g.
{
"header": {
"id":"Identifikation",
"firstName":"First Name",
"lastName":"Last Name"
}
}
How can use such name convention replacement? Do json provide such 'heading' object?

ArrayList on model class

I was just wondering if there is any possibility to create a list or ArrayList on Model class.
Following the data below, as you can see there is ConditionList to pass on Model class.
{
"Id": 30,
"FirstName": "Lilly",
"LastName": "Fowler",
"KnownName": "Lills",
"GenderId": 14,
"DateOfBirth": "2009-03-18T00:00:00",
"**ConditionList**": [{
"Child_ConditionId": 11,
"Name": "Bee Sting Allergy",
"Treatment": "phone me and softly put the bee sting cream on it and smooth it down.The cream will be in her bag",
"DoctorName": null,
"DoctorContact": null,
"MedicationDescription": null
}],
"CreatedOn": "2015-05-30T19:44:13.2",
"UpdatedOn": "2015-06-04T19:29:01.303",
"ProfileImage": "http://mobile.aimy.co.nz:80/Media/mum_cropped_4fe98f04-f772-4ac0-ba51-d99fbd119e4b.jpeg"
}]
#SerializeName("**ConditionList**") private List<ConditionList> conditionList;
Since you are not allowed to use such symbol in Java variable, you should use serializing name that is chipped with Gson

Categories