Java中的JSON(使用JSON.simple)

时间:2014-07-18 12:09:53

标签: java json

我尝试使用JSON.simple创建一个JSON文件,以满足有效负载对服务器的要求。

要求是:

{
    "agent": {
        "name": "String",
        "version": 1
    },
    "username": "String",
    "password": "String",
    "clientToken": "String"
}

还有:

{
  "accessToken": "valid accessToken",
  "clientToken": "client identifier"   
  "selectedProfile": {                  
    "id": "profile identifier",         
    "name": "String"
  }
}

我已经查看了一些帖子,试图找出如何实现这一目标,但我无法做到。所有帮助表示赞赏:)

1 个答案:

答案 0 :(得分:0)

首先创建一个与json字符串匹配的对象。然后你可以使用一些json解析器,如GsonJackson来读取值。例如,第一个json包含一个内部对象代理。因此,我们可以首先为其创建一个Java VO,其字段名称与json字符串匹配。创建第二个对象来保存所有内容。所以它可以被理解为

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

class Agent {
    private String name;
    private int version;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getVersion() {
        return version;
    }
    public void setVersion(int version) {
        this.version = version;
    }
    @Override
    public String toString() {
        return "Agent [name=" + name + ", version=" + version + "]";
    }
}

class User {

    private Agent agent;
    private String username;
    private String password;
    private String clientToken;

    public Agent getAgent() {
        return agent;
    }
    public void setAgent(Agent agent) {
        this.agent = agent;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getClientToken() {
        return clientToken;
    }
    public void setClientToken(String clientToken) {
        this.clientToken = clientToken;
    }
    @Override
    public String toString() {
        return "User [agent=" + agent + ", username=" + username
                + ", password=" + password + ", clientToken=" + clientToken
                + "]";
    }

}

public class Sample {

    public static void main(String[] args) {
        String json = "{\"agent\":{\"name\":\"String\",\"version\":1},\"username\":\"String\",\"password\":\"String\",\"clientToken\":\"String\"}";

        //Gson way
        Gson gson = new Gson();
        User user = gson.fromJson(json, User.class);
        System.out.println(user);

        //Jackson way
        ObjectMapper mapper = new ObjectMapper();
        try {
            user = mapper.readValue(json, User.class);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(user);
    }

}