0

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);
4
  • That is not a valid String. Commented Sep 9, 2014 at 11:05
  • This question is already answered on this link. stackoverflow.com/questions/5245840/… Also this is not a valid string as Dennis suggested. Commented Sep 9, 2014 at 11:06
  • use POJO with ObjectMapper. that is quite simple and understandable.. Commented Sep 9, 2014 at 11:13
  • sorry i have made a mistake adding question.. but in my code its proper Commented Sep 9, 2014 at 11:15

4 Answers 4

1

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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); 

2 Comments

i did same thing but its giving an error saying org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
yes i got, i add string table to JSONArray and then retrieved and added to JSONObject
0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.