{
"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
Related
I have a Json object like this in my Postgres DB:
{
"emails": [
{
"email": {
"id": "e8dc927f-679d-496b-85fb-465edf35c676",
"value": "hello#gmail.com"
}
},
{
"email": {
"id": "1b78758a-abc4-46ef-9de9-c999a0c8c418",
"value": "hello1#gmail.com"
}
}
],
"lastName": {
"id": "718109fd-2d00-475a-829a-c8af9a7f0067",
"value": "lastName"
},
"firstName": {
"id": "6c46a5b3-6f89-4692-a214-4943de22018d",
"value": "firstName"
},
}
And so on big json with around 1000 elements, now I want to parse and get the first 500 elements from json and make another json. what I mean by element here is anything which has Id is a element. For example firstName , LastName, email, email are the elements not the emails. I tried Jackson api but couldn't find a way how to count the elements and make a json exactly like above and return. and when I do any modifications in the first 500 elements I should save the Json with edits. Any help is much appreciated. I even tried postgres array_agg(e) function but that is only accepting only array.
Apologies if this is a duplicate post. I am trying to find a string in the following array response basing on conditions specified.
{
"MRData": {
"xmlns": "http://ergast.com/mrd/1.4",
"series": "f1",
"url": "http://ergast.com/api/f1/2016/drivers.json",
"limit": "30",
"offset": "0",
"total": "24",
"DriverTable": {
"season": "2016",
"Drivers": [
{
"driverId": "alonso",
"permanentNumber": "14",
"code": "ALO",
"url": "http://en.wikipedia.org/wiki/Fernando_Alonso",
"givenName": "Fernando",
"familyName": "Alonso",
"dateOfBirth": "1981-07-29",
"nationality": "Spanish"
},
{
"driverId": "bottas",
"permanentNumber": "77",
"code": "BOT",
"url": "http://en.wikipedia.org/wiki/Valtteri_Bottas",
"givenName": "Valtteri",
"familyName": "Bottas",
"dateOfBirth": "1989-08-28",
"nationality": "Finnish"
},
{
"driverId": "button",
"permanentNumber": "22",
"code": "BUT",
"url": "http://en.wikipedia.org/wiki/Jenson_Button",
"givenName": "Jenson",
"familyName": "Button",
"dateOfBirth": "1980-01-19",
"nationality": "British"
}
]
}
}
}
1) I would like to find the permanent number of driverId "alonso" assuming that it doesn't come first always in each request. i.e each time the request is made the arrays reshuffle. the logic here would be to get the array count of the driverId alonso and insert that into the query below
"MRData.DriverTable.Drivers[insert the array count of alonso here].permanentNumber"
2) I would like to get the permanent numbers that are less than 20. I would also like to get the driverIds of the drivers whose permanent numbers are less than 20.
thanks a lot for viewing!
Try to build the Classes "MRData" and "Driver" with all necessary parameters.
and let org.json or GSON do the magic. You should really look at How to parse JSON in Java as Lars mentioned.
got that sorted!
answer to my first question-
public void extraResponseWithInRange(String url) {
Response response = given().when().get(url);
List<Map<String, String>> responseFromArray = JsonPath.parse(response.asString()).read("$.MRData.DriverTable.Drivers[?(#.driverId== 'alonso')]");
for (Map<String, String> rfa : responseFromArray) {
assertThat(rfa.get("permanentNumber"), equalToIgnoringCase("14"));
answer to my second question-
List<Map<String,String>> driversBetween=JsonPath.parse(response.asString()).read("$.MRData.DriverTable.Drivers[?(#.permanentNumber > '0' && #.permanentNumber <'20')]");
for(Map<String,String> dbsmall: driversBetween){
System.out.println(dbsmall.get("permanentNumber"));
}
please let me know if i could write this in a better way.
thanks a lot!
Either marshall the data into a POJO, and check the values of the fields there, or use something like [JSONPath][1].
int permanentNumber = JSONPath.read(json, "$..Drivers[?(#.driverId == 'alonso')].permanentNumber");
Disclaimer, I don't have an environment currently to run this, but their docs are pretty good.
I need to retrive all contacts with all phone numbers , email ids in a json object like(sample single contact object) :
[{
"id": 123,
"displayName": "Name",
"company": "Company",
"title": "Title",
"numbers": [{
"type": "home",
"number": "957842"
}, {
"type": "work",
"number": "54654654"
}, {
"type": "other",
"number": "465454"
}, {
"type": "other",
"number": "5465431"
}, {
"type": "other",
"number": "54321"
}, {
"type": "other",
"number": "6546545"
}],
"emails": [{
"type": "home",
"email": "asd#gmail.com"
}, {
"type": "work",
"email": "asdas#gmail.com"
}, {
"type": "other",
"email": "asdasd#gmail.com"
}, {
"type": "other",
"email": "sdasdmail.com"
}]
}]
Snippet from my current code looks like :
String searchQuery = "_id > " + lastReadId;
Cursor contactUrl = cContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,searchQuery,null,"_id ASC");
Log.e(TAG, "getContact >> " + contactUrl.getCount());
while (contactUrl.moveToNext())
{
objChild = new JSONObject();
String name=contactUrl.getString(contactUrl.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=contactUrl.getString(contactUrl.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneContactID = contactUrl.getInt(contactUrl.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
int phone_type = contactUrl.getInt(contactUrl.getColumnIndex(Phone.TYPE));
objChild.put("name",name);
objChild.put("number",number);
objChild.put("phone_type", number);
Log.e(TAG, "objChild : " + objChild.toString());
My current contact is like :
http://i.stack.imgur.com/MNuWH.png
http://i.stack.imgur.com/fs8Qk.png
Whatever code I got in stackoverflow was giving me single phone number of contacts or multiple contacts which have multiple numbers(I can manipulate same but it will take good time and may lead to performance issue)
My requirement is to get all phone numbers & emailids of a single contact.
I will suggest you to use Gson in your project for easy JSON parsing
Step 1
Open jsonschema2pojo website.
Ste 2
Copy/Paste your JSON string in given space and select below options to use with Gson.
Enter packagename and classname as per your requirement
select JSON as source type
select Gson as annotation style
Step 3
Click on Preview button to get ready made classes. Copy paste full classes in your application. Else you can also select jar to get classes.
Ste 4
Download and Add Gson library to your project
Download Gson : google-gson
Step 5
Final step. Write below short line of code to convert JSON string to class objects
Gson gson = new Gson();
List<PersonBean> details = gson.fromJson(strJsonData, PersonBean.class);
References :
https://sites.google.com/site/gson/gson-user-guide
https://dzone.com/articles/be-lazy-productive-android
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
This Doc Click here clearly tells me about the merchant authentication. However they only provide the content type : text/xml. Api end point for it is https://apitest.authorize.net/xml/v1/request.api ( sandbox account ).
How can I send a JSON request with name and transaction key as the keys? Also, what is the End Point to which I should send this request for mechant authentication?
Official DOC doesn't help. :'( Was hoping you could. DHC or HttpClientRequester doesn't help too. Getting error.
Authorize.net now works with JSON.
Following is a valid JSON string that you could send to Authorize using whatever platform you use and your own login and secret key.
I should note that even though JSON normally doesn't care which order the values are sent in, Authorize uses a kludgy method to internally convert JSON to XML, so order is in fact, important.
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "yourLoginKey",
"transactionKey": "yourSecretKey"
},
"refId": "12345",
"transactionRequest": {
"transactionType": "authCaptureTransaction",
"amount": "3.99",
"payment": {
"creditCard": {
"cardNumber": "4111111111111111",
"expirationDate": "0522",
"cardCode": "123"
}
},
"customer": {
"id": "John Doe"
},
"billTo": {
"firstName": "John",
"lastName": "Doe",
"address": "123 My Street",
"city": "Chicago",
"state": "IL",
"zip": "60007",
"country": "USA"
}
}
}
}
In my project when i send Rest Response to Advance Rest Client It only shows Fields which Have some values and Ignores(Does not show) fields which have NULL Values or Empty values.
Part Of Code:
Gson gson=new Gson();
// firstResponse is Object which contains the values
String jsonString = gson.toJson(firstResponse);
test.saveJson(jsonString); //OR System.out.println(jsonString);
return Response.ok(firstResponse).build(); // Response to Rest Client
Response sample To return Response.ok(firstResponse).build();
Advance rest client From web project :
{
"Name": "smith",
"Properties": {
"propertyList": [
{
"ID": "072",
"Number": "415151",
"Address": "Somewhere"
},
{
"ID": "151",
"Number": "a800cc79-99d1-42f1-aeb4-808087b12c9b",
"Address": "ninink"
},
{
"ID": "269",
},
],
},
}
Now when i save this as Json String in DB or When i want to Print this to console it also prints the fiels with null or empty values:
{
"Name": "smith",
"Properties": {
"propertyList": [
{
"ID": "072",
"Number": "415151",
"Address": "Somewhere"
},
{
"ID": "151",
"Number": "a800cc79-99d1-42f1-aeb4-808087b12c9b",
"Address": "ninink"
},
{
"ID": "269",
"Number": "",
"Address": ""
},
],
},
"resultList" :[]
}
How can i print or save this JSON string same as response in rest client i.e. i dont want to print null or empty value field i just want to ignore them.
in top og entity class , try with the annotation
#JsonInclude(Include.NON_EMPTY)
this annotation don't show any empty field in your json.
Not giving you a code but here are some pointers for you:
Read the manual: How to handle NULL values
You may need to use a custom exclusion strategy
Also read this Q&A: Gson: How to exclude specific fields from Serialization without annotations