如何从jsonobject获取String数据

时间:2017-07-29 15:24:05

标签: android android-volley

我有一个像下面这样的json数组,想要从形式获取名字和姓氏,请提供guzs的任何帮助

{
    "message": "Verification",
    "data": {
        "accountStatus": "OPEN",
        "firstName": "Y",
        "lastName": "SHITTU",
        "customerType": "SUD",
        "invoicePeriod": 1,
        "dueDate": "2017-09-09T00:00:00+01:00",
        "customerNumber": 309804811
    },
    "status": "success"
}

2 个答案:

答案 0 :(得分:2)

//First create JSON object from the json...    
JSONObject obj = new JSONObject(" .... ");
//Then get firstname and lastname as string...
String firstName = obj.getJSONObject("data").getString("firstName");
String lastName = obj.getJSONObject("data").getString("lastName");

答案 1 :(得分:0)

我建议你先阅读Json Parsing。刚刚用谷歌搜索了tutorial

解析您在问题中的回答: (响应是你拥有的Json作为String类型)

    JsonObject json = new JsonObject(response);

    JsonObject data = json.getJsonObject("data");

    String firstName = data.getString("firstName");
    String lastName = data.getString("lastName");