基于JSON对象创建HashMap

时间:2016-02-18 03:01:29

标签: java json list hashmap jsonobject

我有一个JSON对象,如下所示:

{
    "username":"bobgreen"
    "forename":"Bob"
    "surname":"O'Conor"
}
{
    "username":"jacksmitd"
    "forename":"Jack"
    "surname":"Smitd"
}
{
    "username":"samson"
    "forename":"Sam"
    "surname":"Son"
}

我不确定如何将此数据放入hashmap

到目前为止,我有这样的事情:

// What is returned from the server in JSON format
JSONObject jsonObj = new JSONObject(serverData); 

我不知道如何从这里开始

抱歉 - 我想将以下值"username""forename""surname"传递给以下构造函数 - Personnel(String username, String forename, String surname) 但我不知道如何做for-loop? 我有类似的东西:

for(int i=0; i<jsonObj.size(); i++){
personnel = new Personnel(jsonObj.get("username"), jsonObj.get("forename"), jsonObj.get("surname"));
}

显然这不会奏效 - 但我希望你明白我想要实现的目标

3 个答案:

答案 0 :(得分:0)

您可以使用GSON jar

在一行中实现此目的
Map<String, Object> retMap = new Gson().fromJson(serverData, new TypeToken<HashMap<String, Object>>() {}.getType());

答案 1 :(得分:0)

我看到了Personnel.java

public class Personnel {

    private String username;

    private String forename;

    private String surname; 

    public Personnel(String username, String forename, String surname) {
        this.username = username;
        this.forename = forename;
        this.surname = surname;
        // TODO Auto-generated constructor stub
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the forename
     */
    public String getForename() {
        return forename;
    }

    /**
     * @param forename the forename to set
     */
    public void setForename(String forename) {
        this.forename = forename;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

}

Test.java,使用fastjson-1.2.6.jar

public class Test {


    public static void main(String[] args) {        

        String str = "[{\"username\": \"bobgreen\", \"forename\": \"Bob\",\"surname\": \"O'Conor\"},{\"username\": \"jacksmitd\", \"forename\": \"Jack\",\"surname\": \"Smitd\"},{\"username\": \"samson\", \"forename\": \"Sam\",\"surname\": \"Son\"}]";

        JSONArray jsonArray = JSONObject.parseArray(str);

        Personnel personnel = null;
        JSONObject joJsonObject = null;
        for (int i = 0; i < jsonArray.size(); i++) {

            joJsonObject = jsonArray.getJSONObject(i);
            personnel = new Personnel(joJsonObject.getString("username"), joJsonObject.getString("forename"), joJsonObject.getString("surname"));
        }



    }

}

答案 2 :(得分:0)

首先编写一个POJO类Personnelkeys

中存在JSONObject所属的属性名称
 //try this Code as your POJO.
public class Personnel {
     private String username;
     private String forename;
     private String surname; 
    public Personnel(String username, String forename, String surname) {
    this.username = username;
    this.forename = forename;
    this.surname = surname;
    // TODO Auto-generated constructor stub
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getForename() {
    return forename;
}
public void setForename(String forename) {
    this.forename = forename;
}
public String getSurname() {
    return surname;
}
public void setSurname(String surname) {
    this.surname = surname;
}

将JSON解析为List&lt;&gt;对象

  

import com.fasterxml.jackson.core.JsonParseException;
  import com.fasterxml.jackson.databind.JsonMappingException;
  import com.fasterxml.jackson.databind.ObjectMapper;

然后创建List<Personnel> obj_ListPersonnel

private ObjectMapper mapper = new ObjectMapper();
   //Single Line Code will change you JSONArray (personnelArray) to a list of class
 obj_ListPersonnel = mapper.readValue(personnelArray, mapper.getTypeFactory().constructCollectionType(List.class, Personnel.class));

我已经使用此代码来解析我的java项目中的json。它已在我的项目中实现。 谢谢