Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中获取JSON数据_Java_Json - Fatal编程技术网

如何在java中获取JSON数据

如何在java中获取JSON数据,java,json,Java,Json,我有一个JSON字符串: [ { "customer": { "created_at": "2014-05-22T18:24:55+05:30", "cust_identifier": null, "description": null, "domains": null, "id": 1000170724, "name": "fr

我有一个JSON字符串:

[
    {
        "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"
        }
    }
]

如何使用java获取id的值?

使用JSONObject和JSONArray

int id = new JSONArray(my_json_string).getJSONObject(0).getJSONObject("customer").getInteger("id");
关于java中JSON的更多信息,请点击此处:

您可以使用谷歌

下面是一个小例子:

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