-5

I have a JSON string:

[
    {
        "customer": {
            "created_at": "2014-05-22T18:24:55+05:30",
            "cust_identifier": null,
            "description": null,
            "domains": null,
            "id": 1000170724,
            "name": "freshdesk",
            "note": null,
            "sla_policy_id": 1000042749,
            "updated_at": "2014-05-22T18:24:55+05:30"
        }
    }
]

How to get the value of id using java?

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Suvo
  • 31
  • 1
  • 2
  • 4

2 Answers2

3

Use JSONObject and JSONArray

int id = new JSONArray(my_json_string).getJSONObject(0).getJSONObject("customer").getInteger("id");

More informations on JSON in java here : http://www.json.org/java/

Magus
  • 14,796
  • 3
  • 36
  • 51
0

You can use Googles Gson.

Here is a little example:

Gson gson = new Gson();
int id;

try {
    // read file
    FileInputStream input = new FileInputStream("cutomer.json");
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));

    // read file as JSON Object
    JsonObject json = gson.fromJson(reader, JsonObject.class);

    // read attribut "customer" as array
    JsonArray customers = json.getAsJsonArray("customer");

    JsonObject cutomer= customers.get(0).getAsJsonObject();

    // Attribute ausgeben z.B.: name, alter und hobbies
    id = customer.get("is").getAsInt());

} catch (FileNotFoundException e) {
    e.printStackTrace();
}
JulianG
  • 1,551
  • 1
  • 19
  • 29