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);
-
That is not a valid String.Dennis Traub– Dennis Traub2014年09月09日 11:05:20 +00:00Commented 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.Sneh– Sneh2014年09月09日 11:06:07 +00:00Commented Sep 9, 2014 at 11:06
-
use POJO with ObjectMapper. that is quite simple and understandable..subash– subash2014年09月09日 11:13:50 +00:00Commented Sep 9, 2014 at 11:13
-
sorry i have made a mistake adding question.. but in my code its properuser2894607– user28946072014年09月09日 11:15:35 +00:00Commented Sep 9, 2014 at 11:15
4 Answers 4
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();
Comments
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
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
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...