For example, I have a JSON database like this:
[
{
"address": "America",
"location": {
"lng": 106.669633,
"lat": 10.771883
},
"name": "724 (100) Pyrmon"
},
{
"address": "America",
"location": {
"lng": 106.668821,
"lat": 10.767997
},
"name": "900 (100) Glassdor"
},
{
"address": "328A, Đường Lê Hồng Phong, Ward 1, Quận 10, TP.HCM, Việt Nam",
"location": {
"lng": 106.676276,
"lat": 10.763239
},
"name": "Booking Office Of Phuong Trang Buslines"
},
{
"address": "America",
"location": {
"lng": 106.690329,
"lat": 10.768556
},
"name": "Opposite Pyrmont"
},
{
"address": "America",
"location": {
"lng": 106.691759,
"lat": 10.763937
},
"name": "150 Washington"
},
{
"address": "America",
"location": {
"lng": 106.681683,
"lat": 10.76965
},
"name": "596 - 598 Nguyễn Đình Chiểu"
},
{
"address": "America",
"location": {
"lng": 106.690158,
"lat": 10.77509
},
"name": "17 Bà Huyện Thanh Quan"
},
{
"address": "America",
"location": {
"lng": 106.699749,
"lat": 10.774052
},
"name": "101 Jase"
},
{
"address": "America",
"location": {
"lng": 106.682621,
"lat": 10.778547
},
"name": "274 Lost Region"
},
{
"address": "America",
"location": {
"lng": 106.67305,
"lat": 10.765617
},
"name": "66 Office Base"
},
{
"address": "America",
"location": {
"lng": 106.689733,
"lat": 10.763817
},
"name": "118 Yen The"
},
{
"address": "America",
"location": {
"lng": 106.69313,
"lat": 10.765497
},
"name": "112 Glangdie"
},
]
Now I have to make an algorithm (in any programming language, if in Java, it will be good) that will input the latitude, longitude, and the distance D . The output will be JSON data that contain the information of nearby places that have the distance to the input distance smaller than D .
I have researched about this algorithm, especially on Wikipedia, http://en.wikipedia.org/wiki/Geographical_distance#Singularities_and_discontinuity_of_latitude.2Flongitude.
But I cannot find a way to query from the database, if the database is BIG, I have to query each row(or each JSON Object in this case) to compare with the chosen location, that will be a very slow performance.
How can I do this?
You can try to reduce comparisons by filtering locations inside circumscribed square around circle with input radius. In SQL it looks like: WHERE lng>=(:lng - :radius) AND lng<=(:lng + :radius) AND lat>=(:lat - :radius) AND lat<=(:lat + :radius), where :lng, :lat - input coordinates, :radius - input radius in radians.
Related
I have this JSON schema. I want to read it line by line and populate all "name" key values to an ArrayList of String until it reaches a key called entity. When it reaches to an entity I want to read the entity value and populate it to a String.After that loop should go through the remains of the schema and read all the "name" and populate to a new ArrayList until it meets another entity and so on...
I've only included a part of the schema it's a valid schema.
{
"entity": "Data",
"xement": [
{
"code": "MA",
"entity": "MH",
"attr": [
{
"name": "Id",
"position": 1
},
{
"name": "mdc",
"position": 4
},
{
"name": "minr",
"position": 10
},
{
"name": "mx",
"position": 11
}
],
"maltr": [
{
"name": "sub",
"position": 13
}
]
},
{
"code": "war",
"entity": "subl",
"attr": [
{
"name": "se",
"position": 1
},
{
"name": "go",
"position": 2
},
{
"name": "re",
"position": 4
},
{
"name": "pr",
"position": 10
},
{
"name": "mxp",
"position": 11
},
{
"name": "rpl",
"position": 45
},
{
"name": "rtr",
"position": 47
},
{
"name": "net",
"position": 55
}
],
"groups": [
{
"entity": "ro",
"grattr": [
{
"name": "rmn",
"position": 5
},
{
"name": "aib",
"position": 6
},
{
"name": "nxr",
"position": 7
},
{
"name": "xer",
"position": 8
},
{
"name": "rog",
"position": 9
},
{
"name": "ccc",
"position": 16
}
]
}
]
}
]
}
What is the best way to do that?
I have a requirement to use flattened structure JSON, for example below hierarchical Json:
{
"employees": [
{
"employee1": {
"employeeId": 123,
"name": "ABC",
"type": "permanent",
"address": {
"street": "",
"city": "",
"zipcode": 123456
},
"phoneNumbers": [
123456,
987654
],
"designation": "Manager",
"properties": {
"age": "29 years",
"joiningDate": "17-may-2017",
"salary": "1000 USD"
}
}
},
{
"employee2": {
"employeeId": 123,
"name": "XYZ",
"type": "parttime",
"address": {
"street": "",
"city": "",
"zipcode": 345645
},
"phoneNumbers": [
345332,
675444
],
"designation": "Contractor",
"properties": {
"age": "35 years",
"joiningatDate": "17-june-2015",
"salary": "700 USD"
}
}
}
]
}
**Could be represented as flat structure Json as below(generated this using json-flattener):**
{
"employees[0].employee1.address.zipcode": 123456,
"employees[0].employee1.address.city": "",
"employees[0].employee1.address.street": "",
"employees[0].employee1.name": "ABC",
"employees[0].employee1.employeeId": 123,
"employees[0].employee1.designation": "Manager",
"employees[0].employee1.type": "permanent",
"employees[0].employee1.phoneNumbers[0]": 123456,
"employees[0].employee1.phoneNumbers[1]": 987654,
"employees[0].employee1.properties.joiningDate": "17-may-2017",
"employees[0].employee1.properties.salary": "1000 USD",
"employees[0].employee1.properties.age": "29 years",
"employees[1].employee2.address.zipcode": 345645,
"employees[1].employee2.address.city": "",
"employees[1].employee2.address.street": "",
"employees[1].employee2.name": "XYZ",
"employees[1].employee2.employeeId": 123,
"employees[1].employee2.designation": "Contractor",
"employees[1].employee2.type": "parttime",
"employees[1].employee2.phoneNumbers[0]": 345332,
"employees[1].employee2.phoneNumbers[1]": 675444,
"employees[1].employee2.properties.joiningDate": "17-june-2015",
"employees[1].employee2.properties.salary": "700 USD",
"employees[1].employee2.properties.age": "35 years"
}
My problem is if my service receives flattened Json like above, how to convert it to java domain objects automatically like:
class Employee{
Address address;
Properties property;
}
Does any Java Json parser supports this automatic conversion or i will have to implement own parsing logic splitting the keys based on dot in the keys?
Thanks in Advance.
You can take a look at json-flattener.
It does what you want, and even more.
The usage is simple as this:
String json = "{ \"a\" : { \"b\" : 1, \"c\": null, \"d\": [false, true] }, \"e\": \"f\", \"g\":2.3 }";
String jsonStr = JsonFlattener.flatten(json);
System.out.println(jsonStr);
// Output: {"a.b":1,"a.c":null,"a.d[0]":false,"a.d[1]":true,"e":"f","g":2.3}
I have the following JSON:
{
"items": [
{
"id": "1",
"name": "John",
"location": {
"town": {
"id": "10"
},
"address": "600 Fake Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "2",
"name": "Sarah",
"location": {
"town": {
"id": "10"
},
"address": "76 Evergreen Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "3",
"name": "Hamed",
"location": {
"town": {
"id": "20"
},
"address": "50 East A Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
}
]
}
And I need to get something like this, count how many times each townId appears:
[ { "10": 2 }, {"20": 1 }]
I'm trying to find the most eficient way to do this. Any idea?
Most efficient way is to load the String in a StringBuilder and remove all line breaks and white spaces. Then search for index of "town":{"id":" string (town start index) and then search for the end index (String `"}'). Using the 2 indexes you can extract town ids and count them.
No need to deserialize the JSON into POJO objects:) and extract values by xpath from the POJOs.
I would like to add Junit test for if else statement using assertEquals in android.
My JSON Data
"Main": {
"country": "USA",
"State": "California",
"City": "San Jose"
"Temp" "-10"
},
{
"country": "USA",
"State": "Texas",
"City": "Austin"
"Temp" "10"
},
{
"country": "USA",
"State": "California",
"City": "Cupertino"
"Temp" "00"
},
I am displaying this data base on conditions as below:
if (getTemp!=null && getTemp().getFormatted().startWith("-")) {
displayNegetive.getTemp();
}
else if (getTemp!=null && getTemp().getFormatted().contentEquals("00")) {
displayZero.getTemp();
}
else {
displayPositive.getTemp();
}
I have this json string:
I want to extract all the ids that are after the node of number:"0","1","2"...etc.
I have succeeded to get a single id by using jsonpath: $.response.data.0.id and got "15124".
but i'm looking for a jsonpath that will extract all the ids in the Json String.
in other words this is the expexted output: 15124,13498,14296,13364,14060,13672.
This is the Json String i have:
{
"response": {
"code": 200,
"msg": "Success",
"data": {
"0": {
"id": "15124",
"name": " yoav (yoavshaki#yahoo.com) - 301519506662355",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 3,
"facebook_code": 70
}
},
"1": {
"id": "13498",
"name": "(Not in used) Daniel - 30138444",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"2": {
"id": "14296",
"name": "Daniel - ComeOn (bingocafe#walla.com - 1375713835981039)",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"3": {
"id": "13364",
"name": "Erez - 116060088528093",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"4": {
"id": "14060",
"name": "Erez - NordicBet (gianniciano82#gmail.com - 105134566315107)",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 3,
"facebook_code": 70
}
},
"5": {
"id": "13672",
"name": "Erez - alon.dan - 1378526859026272",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
}
}
}
}
Thanks for all the helpers!
GSON library is a good option to convert java object to json string and vise versa.
for converting json to java object use: fromJson(String, Class)
for converting java object to json string use: toJson(Object)
Here is the sample code using [Gson#fromJson()] to convert JSON string into java Map.
Find more examples...
Sample code:
BufferedReader reader = new BufferedReader(new FileReader(new File("resources/json.txt")));
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Map<String, Object>>>() {}.getType();
Map<String, Map<String, Object>> map = gson.fromJson(reader, type);
Map<String, Map<String, Object>> innerMap = (Map<String, Map<String, Object>>) map.get("response").get("data");
for (String key : innerMap.keySet()) {
System.out.println("key:" + key + " id:" + innerMap.get(key).get("id"));
}
output:
key:0 id:15124
key:1 id:13498
key:2 id:14296
key:3 id:13364
key:4 id:14060
key:5 id:13672
Thanks Syam S. for your answer.
$.response.data.*.id indeed work!