-1
[
{
    "boylam":31.8039,
    "enlem":40.5906,
    "il":"",
    "ilPlaka":"",
    "ilce":"",
    "oncelik":0,
    "yukseklik":2052,
    "aciklama":"",
    "modelId":124774,
    "gps":0
}]

Hi I have such a JSON data in my hand. I had a hard time getting the data out of here. For example, how do I print the "boylam" option in JSON data?

berkancetin
  • 315
  • 1
  • 3
  • 18
jrmm
  • 43
  • 1
  • 5

3 Answers3

1

You can use JSON.simple to convert string data to JSON objects. https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1

public static void main(String[] args) throws ParseException {

    String data = "[\n" +
            "{\n" +
            "    \"boylam\":31.8039,\n" +
            "    \"enlem\":40.5906,\n" +
            "    \"il\":\"\",\n" +
            "    \"ilPlaka\":\"\",\n" +
            "    \"ilce\":\"\",\n" +
            "    \"oncelik\":0,\n" +
            "    \"yukseklik\":2052,\n" +
            "    \"aciklama\":\"\",\n" +
            "    \"modelId\":124774,\n" +
            "    \"gps\":0\n" +
            "}]";

    JSONParser parser = new JSONParser();

    JSONArray jsonArray = (JSONArray) parser.parse(data);

    JSONObject jsonObject = (JSONObject) jsonArray.get(0);

    System.out.println(jsonObject.get("boylam"));

}
lahiruk
  • 433
  • 3
  • 13
0

Use Google Gson,the code maybe like this:

    // construct the json object
    JsonArray jsonArray = new JsonArray();
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("boylam",31.8039);
    jsonArray.add(jsonObject);
    // iterate the json array
    jsonArray.forEach(jsonElement -> {
        JsonObject element = (JsonObject) jsonElement;
        System.out.println(element.get("boylam"));
    });
TongChen
  • 1,414
  • 1
  • 11
  • 21
0

using com.google.gson.Gson class, you can do this.

Gson gson= new Gson();

JSONObject json=new JSONObject();
json.put("boylam", "31.8039"); // this is your json object

Map map=gson.fromJson(json.toString(), Map.class);
System.out.println(map.get("boylam")); //ouput 31.8039
karthick S
  • 564
  • 1
  • 9
  • 18