如何在json中从值获取子字段?

时间:2017-06-17 02:56:04

标签: java json

让我说我有

{
    "name": "Json",
    "detail": {
        "first_name": "Json",
        "last_name": "Scott",
        "age": "23"
    },
    "status": "success"
}

如果它是“成功”,如果它是“终端”或“正在运行”,我希望通过状态获得名称“Json”我不想得到名称。这怎么可以实现?

3 个答案:

答案 0 :(得分:0)

将您的JSON转换为JSONObject,然后您可以通过其名称获取每个值:

JSONObject obj = new JSONObject(yourJsonAsString);
String result = yourJsonObject.getString("name");

您可以先以相同的方式获取状态对象,然后检查结果,然后获取/不相应的名称。

答案 1 :(得分:0)

其中一种方法:

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonParser1 {

    static String js1 = "{\n"
            + "    \"name\": \"Json\",\n"
            + "    \"detail\": {\n"
            + "        \"first_name\": \"Json\",\n"
            + "        \"last_name\": \"Scott\",\n"
            + "        \"age\": \"23\"\n"
            + "    },\n"
            + "    \"status\": \"success\"\n"
            + "}";

    public JsonParser1() {
        parserJSON();
    }


    public void parserJSON() {
        JSONParser parser = new JSONParser();

        try {
            Object obj1 = parser.parse(js1);
            System.out.println("User 1: " + obj1.toString());
            System.out.println();

            JSONObject jobj1 = (JSONObject) obj1;
            String name = jobj1.get("name").toString();
            String status = jobj1.get("status").toString();
            if (name.equals("Json")) {
                System.out.println("Name: " + name + "\t" + "Status: " + status);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new JsonParser1();
    }

}

<强>输出:

User 1: {"name":"Json","detail":{"last_name":"Scott","first_name":"Json","age":"23"},"status":"success"}

Name: Json  Status: success

答案 2 :(得分:0)

试一下

  1. 创建类文件以从JSON获取对象。 (如下所示)

    class ResponseObject implements Serializable {
    
    String name;
    Detail detail;
    String status;
    
    public ResponseObject() {
        this("", new Detail("", "", ""), "");
    }
    
    public ResponseObject(String name, Detail detail, String status) {
        this.name = name;
        this.detail = detail;
        this.status = status;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Detail getDetail() {
        return detail;
    }
    
    public void setDetail(Detail detail) {
        this.detail = detail;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    }
    
    class Detail implements Serializable {
    String first_name;
    String last_name;
    String age;
    
    public Detail() {
        this("", "", "");
    }
    
    public Detail(String first_name, String last_name, String age) {
        this.first_name = first_name;
        this.last_name = last_name;
        this.age = age;
    }
    
    public String getFirst_name() {
        return first_name;
    }
    
    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }
    
    public String getLast_name() {
        return last_name;
    }
    
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
    
    public String getAge() {
        return age;
    }
    
    public void setAge(String age) {
        this.age = age;
    }
    }
    
  2. 解析JSON字符串并使用谷歌GSON获取对象如下所示。

    String strResponse = "{ \"name\": \"Json\", \"detail\": { \"first_name\": \"Json\", \"last_name\": \"Scott\", \"age\": \"23\" }, \"status\": \"success\" }";
    
    
    try {
    
        ResponseObject responseObject = new Gson().fromJson(strResponse, ResponseObject.class);
    
        if (responseObject.getStatus().equalsIgnoreCase("success")) {
    
            System.out.println(responseObject.getName());
    
            System.out.println(responseObject.getDetail().getFirst_name());
    
        } else {
    
            //status terminal
    
        }
    
    
    } catch (JsonSyntaxException e) {
        e.printStackTrace();
    }