Finding latitude and longitude of a known place using api? [duplicate] - java

This question already has answers here:
Get latitude and longitude based on location name with Google Autocomplete API
(7 answers)
Closed 10 years ago.
Is there a way by which I can find latitude and longitude of a known place e.g. known city like Singapore, Kuala Lumpur / New York.

Geocoding
Check out: Google Geocoding API

Ask google.
e.g.
This http request:
http://maps.google.com/maps/geo?q=Singapore
returns:
{
"name": "Singapore",
"Status": {
"code": 200,
"request": "geocode"
},
"Placemark": [ {
"id": "p1",
"address": "Singapur",
"AddressDetails": {
"Accuracy" : 1,
"Country" : {
"CountryName" : "Singapur",
"CountryNameCode" : "SG"
}
},
"ExtendedData": {
"LatLonBox": {
"north": 1.4708809,
"south": 1.1663980,
"east": 104.0856805,
"west": 103.6056246
}
},
"Point": {
"coordinates": [ 103.8198360, 1.3520830, 0 ]
}
} ]
}

Related

How do I exit a JSON array and enter next object [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
How to parse json parsing Using GSON in android
(5 answers)
How to parse JSON in Java
(36 answers)
Closed 10 months ago.
Below is my JSON format, currently my code is in the currencies, and has obtained the currency code successfully, but now i am stuck in terms on how to exit this array and go to languages object.
My current code basically obtains the name of the country, capital and region from the JSON code stuff attached. I also figured out for it to obtain the currency code, but now I want to get the languages part. This is all done within a while loop in java on AndroidStudio.
[
{
"name":"China",
"topLevelDomain":[
".cn"
],
"alpha2Code":"CN",
"alpha3Code":"CHN",
"callingCodes":[
"86"
],
"capital":"Beijing",
"altSpellings":[
"CN",
"Zhōngguó",
"Zhongguo",
"Zhonghua",
"People's Republic of China",
"中华人民共和国",
"Zhōnghuá Rénmín Gònghéguó"
],
"subregion":"Eastern Asia",
"region":"Asia",
"population":1402112000,
"latlng":[
35.0,
105.0
],
"demonym":"Chinese",
"area":9640011.0,
"gini":38.5,
"timezones":[
"UTC+08:00"
],
"borders":[
"AFG",
"BTN",
"MMR",
"HKG",
"IND",
"KAZ",
"PRK",
"KGZ",
"LAO",
"MAC",
"MNG",
"PAK",
"RUS",
"TJK",
"VNM",
"NPL"
],
"nativeName":"中国",
"numericCode":"156",
"flags":{
"svg":"https://flagcdn.com/cn.svg",
"png":"https://flagcdn.com/w320/cn.png"
},
"currencies":[
{
"code":"CNY",
"name":"Chinese yuan",
"symbol":"¥"
}
],
"languages":[
{
"iso639_1":"zh",
"iso639_2":"zho",
"name":"Chinese",
"nativeName":"中文 (Zhōngwén)"
}
],
"translations":{
"br":"China",
"pt":"China",
"nl":"China",
"hr":"Kina",
"fa":"چین",
"de":"China",
"es":"China",
"fr":"Chine",
"ja":"中国",
"it":"Cina",
"hu":"Kína"
},
"flag":"https://flagcdn.com/cn.svg",
"cioc":"CHN",
"independent":true
},
{
"name":"Macao",
"topLevelDomain":[
".mo"
],
"alpha2Code":"MO",
"alpha3Code":"MAC",
"callingCodes":[
"853"
],
"altSpellings":[
"MO",
"澳门",
"Macao Special Administrative Region of the People's Republic of China",
"中華人民共和國澳門特別行政區",
"Região Administrativa Especial de Macau da República Popular da China"
],
"subregion":"Eastern Asia",
"region":"Asia",
"population":649342,
"latlng":[
22.16666666,
113.55
],
"demonym":"Chinese",
"area":30.0,
"timezones":[
"UTC+08:00"
],
"borders":[
"CHN"
],
"nativeName":"澳門",
"numericCode":"446",
"flags":{
"svg":"https://flagcdn.com/mo.svg",
"png":"https://flagcdn.com/w320/mo.png"
},
"currencies":[
{
"code":"MOP",
"name":"Macanese pataca",
"symbol":"P"
}
],
"languages":[
{
"iso639_1":"zh",
"iso639_2":"zho",
"name":"Chinese",
"nativeName":"中文 (Zhōngwén)"
},
{
"iso639_1":"pt",
"iso639_2":"por",
"name":"Portuguese",
"nativeName":"Português"
}
],
"translations":{
"br":"Macau",
"pt":"Macau",
"nl":"Macao",
"hr":"Makao",
"fa":"مکائو",
"de":"Macao",
"es":"Macao",
"fr":"Macao",
"ja":"マカオ",
"it":"Macao",
"hu":"Makaó"
},
"flag":"https://flagcdn.com/mo.svg",
"independent":false
},
{
"name":"Taiwan",
"topLevelDomain":[
".tw"
],
"alpha2Code":"TW",
"alpha3Code":"TWN",
"callingCodes":[
"886"
],
"capital":"Taipei",
"altSpellings":[
"TW",
"Táiwān",
"Republic of China",
"中華民國",
"Zhōnghuá Mínguó"
],
"subregion":"Eastern Asia",
"region":"Asia",
"population":23503349,
"latlng":[
23.5,
121.0
],
"demonym":"Taiwanese",
"area":36193.0,
"timezones":[
"UTC+08:00"
],
"nativeName":"臺灣",
"numericCode":"158",
"flags":{
"svg":"https://flagcdn.com/tw.svg",
"png":"https://flagcdn.com/w320/tw.png"
},
"currencies":[
{
"code":"TWD",
"name":"New Taiwan dollar",
"symbol":"$"
}
],
"languages":[
{
"iso639_1":"zh",
"iso639_2":"zho",
"name":"Chinese",
"nativeName":"中文 (Zhōngwén)"
}
],
"translations":{
"br":"Taiwan",
"pt":"Taiwan",
"nl":"Taiwan",
"hr":"Tajvan",
"fa":"تایوان",
"de":"Taiwan",
"es":"Taiwán",
"fr":"Taïwan",
"ja":"台湾(中華民国)",
"it":"Taiwan",
"hu":"Tajvan"
},
"flag":"https://flagcdn.com/tw.svg",
"cioc":"TPE",
"independent":true
}
]

How to create an object class that can converted to json like this in java? [duplicate]

This question already has answers here:
Is there a way to create the bean class from a json response
(6 answers)
Closed 3 years ago.
This is the JSON request that I need to pass to other API, My problem is I dont know how to create an object that can have other object inside.
What is the best approach for this, I'll be using ObjectMapper to convert the object into json like this.
{
"request": {
"head": {
"version": "1.0.0",
"function": "sample.function",
"clientId": "clientId",
"clientSecret": "clientSecret",
"reqTime": "2001-07-04T12:08:56+05:30",
"reqMsgId": "reqMessageID",
"reserve": ""
},
"body": {
"occurTime": "2018-03-04T12:08:56+08:00",
"bizScene": "SAMPLE_BIZ",
"envInfo": {
"tokenId": "jkahsdhsjakdhkjsajdsahdkjsakdhsa===",
"clientIp": "127.0.0.1",
"appVersion": "v0.1.0",
"terminalType": "NOP"
},
"extendInfo": {
"PSID":"87943297427",
"PSID_CreatedDate":"2018-01-04T12:08:56+08:00",
"mobtelLinkingDate" : "2018-03-04T12:08:56+08:00",
"mobtelUnlinkingDate" : "2018-04-04T12:08:56+08:00",
"activeLinkedMobtel" : "123756"
},
"accountInfo": {
"userMobile": "123756"
},
"operationInfoDTO" : {
"operationOrigin": "TWEETER",
},
"operationType": "SAMPLE",
"operationResult": "FALSE",
}
},
"signature": "signature string"
}
I gave a name to the root Object a used this website to generate boilerplate bean object
"rootObj":{
"request": {
"head": {
"version": "1.0.0",
"function": "sample.function",
"clientId": "clientId",
"clientSecret": "clientSecret",
"reqTime": "2001-07-04T12:08:56+05:30",
"reqMsgId": "reqMessageID",
"reserve": ""
},
"body": {
"occurTime": "2018-03-04T12:08:56+08:00",
"bizScene": "SAMPLE_BIZ",
"envInfo": {
"tokenId": "jkahsdhsjakdhkjsajdsahdkjsakdhsa===",
"clientIp": "127.0.0.1",
"appVersion": "v0.1.0",
"terminalType": "NOP"
},
"extendInfo": {
"PSID":"87943297427",
"PSID_CreatedDate":"2018-01-04T12:08:56+08:00",
"mobtelLinkingDate" : "2018-03-04T12:08:56+08:00",
"mobtelUnlinkingDate" : "2018-04-04T12:08:56+08:00",
"activeLinkedMobtel" : "123756"
},
"accountInfo": {
"userMobile": "123756"
},
"operationInfoDTO" : {
"operationOrigin": "TWEETER",
},
"operationType": "SAMPLE",
"operationResult": "FALSE",
}
},
"signature": "signature string"
}
There are some minor syntax errors in your JSON example in line 33 and 36.
JSON doesn't like comma after the last element of a list or dictionary.
If you remove those and use this JSON as input for http://www.jsonschema2pojo.org/ you'll get corresponding Java classes.

How to Store Complex Json in Redis using ReJson

I need to store java object (may be json formatted) in Redis. I was searching over internet and found ReJson module.
{
"site": "sddd",
"pConfig" : {
"floatpoint" : "http://10.32.3.36:8003",
"user" : "root",
"password" : "xxx"
},
"Config": {
"initInSecs": 0,
"checkInSecs": 29
},
"refC": {
"initSecs": 0,
"InSecs": 59,
"InSecsOnDown": 15,
"InMillis" : 5000,
"endPoints": [
{
"ip": "10.32.17.66",
"port": "22"
},
{
"ip": "10.32.17.66",
"port": "21"
}
]
},
"syncWConfig": {
"initDelayInSecs": 0
}
}
Can you please help how to store this Json using ReJson. I also want to retrieved elements and its values. Can you help with small code snippet.
You should check JRedisJSON java client
https://github.com/RedisJSON/JRedisJSON
As for search and secondary index support it should be available soon for RedisJSON see https://github.com/RedisJSON/RedisJSON2

Jayway JsonPath filtering with predicates

I've recently taken up Jayway JsonPath and I've had trouble with how the inpath filtering works.
So my JSON looks like this:
At the top I have shareables. These shareables have an array called user, which contains an ID and a name, and they also contain an item called dataset, which can contain any json.
These shareables can exist within the dataset as well.
My working JSON looks like this:
{
"shareable": {
"user": [
{
"ID": 1,
"Name": "Bob"
},
{
"ID": 2,
"Name": "Charles"
}
],
"dataSet": [
{
"insulinMeasurement":
{
"timestamp": "Tuesday Morning",
"measurement": 174,
"unit": "pmol/L"
}
},
{
"insulinMeasurement":
{
"timestamp": "Tuesday Noon",
"measurement": 80,
"unit": "pmol/L"
}
},
{ "shareable": {
"user": [
{
"ID": 3,
"Name": "Jim"
}
],
"dataSet": [
{
"insulinMeasurement":
{
"timestamp": "Tuesday Evening",
"measurement": 130,
"unit": "pmol/L"
}
}
]
}
},
{ "unshareable": {
"user": [
{
"ID": 2,
"Name": "Bob"
}
],
"dataSet": [
{
"insulinMeasurement":
{
"timestamp": "Tuesday Night",
"measurement": 130,
"unit": "pmol/L"
}
}
]
}
}
]
}
}
So what I want is, all shareables that have a user with a certain ID. So I figured the path I would use would look like this:
$..shareable[ ?(#.user[*].ID == 1 )]
which here has a hardcoded ID. This returns nothing while
$..shareable[ ?(#.user[0].ID == 1 )]
returns any shareable where the first ID is 1.
I also tried something along the lines of
$..shareable[ ?(#.user[?(#.ID == 1)]
which I figure should return any shareable that has a user with an ID of 1.
Am I going about this the wrong way? Do I need to somehow iterate through the user objects that exist?
Well, I figured it out, so if anyone stumbles across this, the query should look as follows:
$..shareable[?( " + user + " in #.user[*].ID )]
where user is just the int of the userId. Basically the right hand side creates a list of all IDs that shareable contains, and checks if the requested ID exists therein.

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");
}
}

Categories