-2

My goal is to parse a json file with Java.

My json file looks like this:

{"11542": [40.870932001722714, -73.62889780791781], "54548": [45.859817510232425, -89.82102639934573], "11547": [40.83072033793459, -73.6445076194238]}

What I wanna do is to be able to input the zip code (the string) and get the coordinates as outcome

Thank you

Edit:

TypeToken helped a lot!!

public class ZipCodeLookup {

public class ZipCodeResult {

    final double longitude;
    final double latitude;

    public ZipCodeResult(double longitude, double latitude) {
        this.longitude = longitude;
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

}

public Map<String, Double[]> lookups;

public ZipCodeLookup(InputStream is) throws IOException {
    Gson gson = new Gson();
    Reader reader = new InputStreamReader(is);
    lookups = gson.fromJson(reader, new TypeToken<Map<String, Double[]>>() {
    }.getType());

    reader.close();
}

public ZipCodeResult lookupZipcode(String zipcode) {
    Double[] values = lookups.get(zipcode);
    return (values == null) ? null : new ZipCodeResult(values[0], values[1]);
}

}

drillau
  • 25
  • 1
  • 10

3 Answers3

0

There are several posts here that will help you to solve your problem.

How to parse JSON

Edit:

Based on your comment. This is a simple example (not tested)

String str = "{yourJson}";
JSONObject obj = new JSONObject(str);
JSONArray arr = obj.getJSONArray("zip-code");
arr.getDouble(0); // Coordinate X
arr.getDouble(1); // Coordinate Y

http://theoryapp.com/parse-json-in-java/

Esteban Garcia
  • 2,171
  • 16
  • 24
  • yes but all the examples I looked up had things like name, email etc. my json file looks like this https://raw.githubusercontent.com/openeemeter/eemeter/master/eemeter/resources/zipcode_centroid_lat_lngs.json – drillau Jan 16 '18 at 15:28
0

Using json.org you can easily access data like:

    JSONObject json= new JSONObject(string); //String is the json you want to parse
    int firstNumber=json.getJSONArray("11542").getInt(0); //11542 is the zip code you want to access
    int secondNumber=json.getJSONArray("11542").getInt(1);
Daniel Diment
  • 110
  • 1
  • 8
0

You should add more details in your questions. Anyway, you can use simple JSON and then following code:

JSONParser parser = new JSONParser();

try {
    JSONObject jsonObject = (JSONObject)parser.parse("{\"11542\": [40.870932001722714, -73.62889780791781], \"54548\": [45.859817510232425, -89.82102639934573], \"11547\": [40.83072033793459, -73.6445076194238]}");  
    JSONArray coords = (JSONArray) jsonObject.get(zipCode);
    Iterator<Double> iterator = coords.iterator();
    while (iterator.hasNext()) {
     System.out.println(iterator.next());
    }        
} catch (ParseException e) {
    e.printStackTrace();
}

Of course you have to pass correct zipCode. If you do this you should see the following results:

40.870932001722714
-73.62889780791781

Hope, it helps

Planck Constant
  • 1,406
  • 1
  • 17
  • 19