user2894607
user2894607

Reputation: 175

Convert string to json object using java

I have a string data and i want to add it to json and retrive it to add it to excel. But its not happening Please kindly help this is my code....

String table = "[{"id":1,"name":"xxx","location":"xx"}{"id":2,"name":"yyy","location":"yy"}{"id":3,"name":"zzz","location":"zz"}]";

 JSONObject jObject  = new JSONObject(table); // json       String
 projectname = jObject.getString("name"); // get the name from data.
 System.out.println(projectname);

Upvotes: 0

Views: 1406

Answers (4)

SparkOn
SparkOn

Reputation: 8956

Simple and effective way i will tell you using json.org and jackson-mapper-asl first create a class suppose its name is Person

public class Person {
    private int id;
    private String name;
    private String location;
    //getters and setters
    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", location='" + location + '\'' +
                '}';
    }
}

create another class suppose mainclass

public static void main(String[] args) throws IOException, JSONException {
    String jsonString ="[{\"id\":1,\"name\":\"xxx\",\"location\":\"xx\"},
                                 {\"id\":2,\"name\":\"yyy\",\"location\":\"yy\"},
                                 {\"id\":3,\"name\":\"zzz\",\"location\":\"zz\"}]";
    ObjectMapper mapper = new ObjectMapper();
    JSONArray jsonArray = new JSONArray(jsonString);
    List<Person> listFromJsonArray = new ArrayList<Person>();
    for(int i =0 ;i<jsonArray.length();i++){
        String firstObjectAsString = jsonArray.get(i).toString();
        Person person = mapper.readValue(mapper.readTree(firstObjectAsString), 
                                                                   Person.class);
        listFromJsonArray.add(person);
    }
    System.out.println(listFromJsonArray);

}

Now using the list get the individual Person objects get the individual values using getters and do whatever you want

Upvotes: 1

Hardik Visa
Hardik Visa

Reputation: 323

You can try following code for parsing json object

String table = "{"keyElement" : {"id":1,"name":"xxx","location":"xx"}{"id":2,"name":"yyy","location":"yy"}{"id":3,"name":"zzz","location":"zz"}}";
JSONObject jObject  = new JSONObject(table);
JSONArray array = jObject.getJSONArray("keyElement");    
for(int i = 0 ; i < array.length() ; i++)
{
    System.out.println(array.getJSONObject(i).getString("id"));
    System.out.println(array.getJSONObject(i).getString("name"));
    System.out.println(array.getJSONObject(i).getString("location"));
}

Now You can get idea...

Upvotes: 0

Vipin CP
Vipin CP

Reputation: 3807

You can use JSONObject and JsonArray for this as shown below

JSONObject mainObj = new JSONObject();
    JSONArray somearr = new JSONArray();
                        while (iter.hasnext()) {
                             ClassName someObject=iter.next();
                            JSONObject jsonobj = new JSONObject();
                            jsonobj.put("id", id_from_someObject);;
                            jsonobj.put("name", name_from_someObject);
                            jsonobj.put("name", location_from_someObject);;

                        somearr.put(seatObj);

                        }

mainObj.put("response",somearr);

return mainObj.toString();

Upvotes: 1

Lucaci Andrei
Lucaci Andrei

Reputation: 407

Well, you do have an error at defining your table string. I'm guessing you need

String table = "[{'id':1,'name':'xxx','location':'xx'}{'id':2,'name':'yyy','location':'yy'}{'id':3,'name':'zzz','location':'zz'}]";
JSONObject jObject = new JSONObject(table);

instead of

String table = [{"id":1,"name":"xxx","location":"xx"}{"id":2,"name":"yyy","location":"yy"}{"id":3,"name":"zzz","location":"zz"}];
JSONObject jObject = new JSONObject(table); 

Upvotes: 0

Related Questions