1

I have a JSON Object such as the following:

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

I am not sure how to put this data into the hashmap.

So far I have something like this:

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

I don't know how to proceed from here

SORRY - I want to pass the following values "username", "forename", "surname" into the following constructor - Personnel(String username, String forename, String surname) But I don't know how to do the for-loop? I had something like:

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

Obviously that won't work - but I hope you understand what I am trying to achieve

3 Answers3

0

You can achieve this in one line using GSON jar

Map<String, Object> retMap = new Gson().fromJson(serverData, new TypeToken<HashMap<String, Object>>() {}.getType());
SubSul
  • 2,523
  • 1
  • 17
  • 27
  • What about without the `GSON` library? Would appreciate another alternative please – Samuel Georgeszusz Feb 18 '16 at 03:08
  • Did you get to try any of these methods mentioned here -> http://stackoverflow.com/questions/21720759/convert-a-json-string-to-a-hashmap – SubSul Feb 18 '16 at 03:19
  • I updated my question. It is just how do I pass `username`, `forename`, `surname` into the new constructor everytime? I just need to know how to get the `username`, `forename` and `surname` within every `{}` – Samuel Georgeszusz Feb 18 '16 at 03:22
0

I see, the 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;
    }

}

the Test.java, using 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"));
        }



    }

}
Python Basketball
  • 2,320
  • 3
  • 24
  • 47
  • Thank you, but I resolved my question. Turns out `JSONObject` and `JSONArray` are two completely different things. I guess I learnt something new – Samuel Georgeszusz Feb 18 '16 at 04:03
0

First Write a POJO Class Personnel in which attributes name should be matched as per keys exists in 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;
}

Parsing JSON to List<> Object

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

then Create a 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));

I have used this code to parse json in my java project. It is already implemented in my project. Thank You

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52