I'm just getting started with using json with java. I'm not sure how to access string values within a JSONArray. For instance, my json looks like this:
{
"locations": {
"record": [
{
"id": 8817,
"loc": "NEW YORK CITY"
},
{
"id": 2873,
"loc": "UNITED STATES"
},
{
"id": 1501
"loc": "NEW YORK STATE"
}
]
}
}
my code:
JSONObject req = new JSONObject(join(loadStrings(data.json),""));
JSONObject locs = req.getJSONObject("locations");
JSONArray recs = locs.getJSONArray("record");
I have access to the "record" JSONArray at this point, but am unsure as to how I'd get the "id" and "loc" values within a for loop. Sorry if this description isn't too clear, I'm a bit new to programming.
Have you tried using JSONArray.getJSONObject(int), and JSONArray.length() to create your for-loop:
for (int i = 0; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("id");
String loc = rec.getString("loc");
// ...
}
An org.json.JSONArray is not iterable.
Here's how I process elements in a net.sf.json.JSONArray:
JSONArray lineItems = jsonObject.getJSONArray("lineItems");
for (Object o : lineItems) {
JSONObject jsonLineItem = (JSONObject) o;
String key = jsonLineItem.getString("key");
String value = jsonLineItem.getString("value");
...
}
Works great... :)
Java 8 is in the market after almost 2 decades, following is the way to iterate org.json.JSONArray with java8 Stream API.
import org.json.JSONArray;
import org.json.JSONObject;
#Test
public void access_org_JsonArray() {
//Given: array
JSONArray jsonArray = new JSONArray(Arrays.asList(new JSONObject(
new HashMap() {{
put("a", 100);
put("b", 200);
}}
),
new JSONObject(
new HashMap() {{
put("a", 300);
put("b", 400);
}}
)));
//Then: convert to List<JSONObject>
List<JSONObject> jsonItems = IntStream.range(0, jsonArray.length())
.mapToObj(index -> (JSONObject) jsonArray.get(index))
.collect(Collectors.toList());
// you can access the array elements now
jsonItems.forEach(arrayElement -> System.out.println(arrayElement.get("a")));
// prints 100, 300
}
If the iteration is only one time, (no need to .collect)
IntStream.range(0, jsonArray.length())
.mapToObj(index -> (JSONObject) jsonArray.get(index))
.forEach(item -> {
System.out.println(item);
});
By looking at your code, I sense you are using JSONLIB. If that was the case, look at the following snippet to convert json array to java array..
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input );
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY );
jsonConfig.setRootClass( Integer.TYPE );
int[] output = (int[]) JSONSerializer.toJava( jsonArray, jsonConfig );
In case it helps someone else,
I was able to convert to an array by doing something like this,
JSONObject jsonObject = (JSONObject)new JSONParser().parse(jsonString);
((JSONArray) jsonObject).toArray()
...or you should be able to get the length
((JSONArray) myJsonArray).toArray().length
HashMap regs = (HashMap) parser.parse(stringjson);
(String)((HashMap)regs.get("firstlevelkey")).get("secondlevelkey");
Related
I have an service that returns me an json object like the below
{
"header": {},
"title": {},
"terms": {
"data": {
"list": [
"string": 1,
"string1": 2,
"string2": 3
]
}
}
}
Now I need to get the keys of the list json array into a list. I have got the array into an object
List<String> allTerms = new ArrayList<String>();
String response = HttpRequest.get("http://myservice/get").body();
JSONParser parser = new JSONParser();
Object obj = parser.parse(response);
JSONObject jsonObject = (JSONObject) obj;
JSONObject fieldObj = (JSONObject)jsonObject.get("terms");
JSONObject queryObj = (JSONObject)fieldObj.get("data");
JSONArray termsArr = (JSONArray) queryObj.get("list");
//iterate the termsarr and get the string,string1,string2 keys alone to allTerms list
Is there a more better way to do this? Im using json-simple and a custom http client
By using basic for loop
for(int i=0; i<termsArr.length(); i++) {
String[] arr = termsArr.getString(i).split("\"");
allTerms.add(arr[1]);
}
how can I create a JSON Object like the following, in Java using JSONObject ?
{
"employees": [
{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}
],
"manager": [
{"firstName": "John", "lastName": "Doe"},
{"firstName": "Anna", "lastName": "Smith"},
{"firstName": "Peter", "lastName": "Jones"}
]
}
I've found a lot of example, but not my exactly JSONArray string.
Here is some code using java 6 to get you started:
JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");
JSONArray ja = new JSONArray();
ja.put(jo);
JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);
Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.
An example of this using the builder pattern in java 7 looks something like this:
JsonObject jo = Json.createObjectBuilder()
.add("employees", Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("firstName", "John")
.add("lastName", "Doe")))
.build();
I suppose you're getting this JSON from a server or a file, and you want to create a JSONArray object out of it.
String strJSON = ""; // your string goes here
JSONArray jArray = (JSONArray) new JSONTokener(strJSON).nextValue();
// once you get the array, you may check items like
JSONOBject jObject = jArray.getJSONObject(0);
Hope this helps :)
Small reusable method can be written for creating person json object to avoid duplicate code
JSONObject getPerson(String firstName, String lastName){
JSONObject person = new JSONObject();
person .put("firstName", firstName);
person .put("lastName", lastName);
return person ;
}
public JSONObject getJsonResponse(){
JSONArray employees = new JSONArray();
employees.put(getPerson("John","Doe"));
employees.put(getPerson("Anna","Smith"));
employees.put(getPerson("Peter","Jones"));
JSONArray managers = new JSONArray();
managers.put(getPerson("John","Doe"));
managers.put(getPerson("Anna","Smith"));
managers.put(getPerson("Peter","Jones"));
JSONObject response= new JSONObject();
response.put("employees", employees );
response.put("manager", managers );
return response;
}
Please try this ... hope it helps
JSONObject jsonObj1=null;
JSONObject jsonObj2=null;
JSONArray array=new JSONArray();
JSONArray array2=new JSONArray();
jsonObj1=new JSONObject();
jsonObj2=new JSONObject();
array.put(new JSONObject().put("firstName", "John").put("lastName","Doe"))
.put(new JSONObject().put("firstName", "Anna").put("v", "Smith"))
.put(new JSONObject().put("firstName", "Peter").put("v", "Jones"));
array2.put(new JSONObject().put("firstName", "John").put("lastName","Doe"))
.put(new JSONObject().put("firstName", "Anna").put("v", "Smith"))
.put(new JSONObject().put("firstName", "Peter").put("v", "Jones"));
jsonObj1.put("employees", array);
jsonObj1.put("manager", array2);
Response response = null;
response = Response.status(Status.OK).entity(jsonObj1.toString()).build();
return response;
I'm trying to create a Gson object which will contain differents categories and entries
Here is the sample i'm trying to do:
JsonObject jo = new JsonObject();
JsonArray ja = new JsonArray();
JsonObject mainObj = new JsonObject();
jo.addProperty("firstName", "John");
jo.addProperty("lastName", "Doe");
ja.add(jo);
mainObj.add("employees", ja);
jo = new JsonObject();
ja = new JsonArray();
jo.addProperty("firstName", "jean");
jo.addProperty("lastName", "dorian");
ja.add(jo);
mainObj.add("employees", ja);
jo = new JsonObject();
ja = new JsonArray();
jo.addProperty("firstName", "toto");
jo.addProperty("lastName", "tata");
ja.add(jo);
mainObj.add("manager", ja);
The problem is has you can see I have to create every time a new JSonObject and Array which is I believe not the best practice and also the old value in "employees" is replacing by the second.
Someone can help me on this please?
Br,
Jérémie
I have to create every time a new JSonObject and Array which is I
believe not the best practice
I think it's perfectly fine to do it like this.
and also the old value in "employees" is replacing by the second
The problem is that you want to add a mapping "employees" -> JsonArray two times in your JsonObject. While online JSON parsers such as JSONLint don't say anything about that, it's actually not recommended to have two identical keys in a JsonObject.
This is explained in the RFC 7159, chapter 4:
An object whose names are all unique is interoperable in the sense
that all software implementations receiving that object will agree on
the name-value mappings. When the names within an object are not
unique, the behavior of software that receives such an object is
unpredictable. Many implementations report the last name/value pair
only. Other implementations report an error or fail to parse the
object, and some implementations report all of the name/value pairs,
including duplicates.
Under the hood, the JsonObject structure is implemented with a LinkedTreeMap to save the mappings. When you add a new mapping, the put method is called, which will erase the previous mapped value, if any.
90 #Override public V put(K key, V value) {
91 if (key == null) {
92 throw new NullPointerException("key == null");
93 }
94 Node<K, V> created = find(key, true);
95 V result = created.value;
96 created.value = value;
97 return result;
98 }
If you want to add another Employee to the array, you shouldn't add it directly to the JsonObject and create a new JsonArray.
JsonObject jo = new JsonObject();
JsonArray ja = new JsonArray();
JsonObject mainObj = new JsonObject();
jo.addProperty("firstName", "John");
jo.addProperty("lastName", "Doe");
ja.add(jo);
//remove this line
mainObj.add("employees", ja);
jo = new JsonObject();
//and remove this line
ja = new JsonArray();
jo.addProperty("firstName", "jean");
jo.addProperty("lastName", "dorian");
ja.add(jo);
mainObj.add("employees", ja);
jo = new JsonObject();
ja = new JsonArray();
jo.addProperty("firstName", "toto");
jo.addProperty("lastName", "tata");
ja.add(jo);
mainObj.add("manager", ja);
which will result in:
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "jean",
"lastName": "dorian"
}
],
"manager": [
{
"firstName": "toto",
"lastName": "tata"
}
]
}
I have data like this:
NewsItem :
id
title
date
txt
There may be many NewsItems say 10. I have to send them to jquery.
I am doing this:
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
for(int i = 0 ; i< list.size() ; i++){
p = list.get(i);
arr.put(p.getId());
arr.put(p.getTitle());
arr.put(new MyDateFormatter().getStringFromDateDifference(p.getCreationDate()));
arr.put(getTrimmedText(p.getText()));
obj.put(""+i,arr);
arr = new JSONArray();
}
This will create a JSON string like this : {"1":["id","title","date","txt"],"2":[......and so on...
Is that correct way of doing this?
How can I parse this string so that I can get each news item object in jQuery so that I can access attr.
Like this:
obj.id,
obj.title
Or if this is wrong way of creating JSON string, please suggest some better way with example of parsing in jQuery.
I believe that you're organizing your data backwards. It seems that you want to use an array of NewsItems, and if so, then your java JSON generation code should look like this:
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
for(int i = 0 ; i< list.size() ; i++)
{
p = list.get(i);
obj.put("id", p.getId());
obj.put("title", p.getTitle());
obj.put("date". new MyDateFormatter().getStringFromDateDifference(p.getCreationDate()));
obj.put("txt", getTrimmedText(p.getText()));
arr.put(obj);
obj = new JSONObject();
}
Now your JSON string will look something like this:
[{"id": "someId", "title": "someTitle", "date": "dateString", "txt": "someTxt"},
{"id": "someOtherId", "title": "someOtherTitle", "date": "anotherDateString", "txt": "someOtherTxt"},
...]
Assuming that your NewsItem gettors return Strings. The JSONObject method put is overloaded to take primitive types also, so if, e.g. your getId returns an int, then it will be added as a bare JSON int. I'll assume that JSONObject.put(String, Object) calls toString on the value, but I can't verify this.
Now in javascript, you can use such a string directly:
var arr =
[{"id": "someId", "title": "someTitle", "date": "dateString", "txt": "someTxt"},
{"id": "someOtherId", "title": "someOtherTitle", "date": "anotherDateString", "txt": "someOtherTxt"}];
for (i = 0; i < arr.length; i++)
alert(arr[i].title); // should show you an alert box with each first title
The idea of the json object is the same as a dictionary/map where you have keys and values assigned to those keys, so what you want to construct would be something like this:
{"1": {"title": "my title", "date": "17-12-2011", "text": "HELLO!"}, "2": ....}
where the "1" is the id and the contents is another dictionary/map with the info.
lets say you assigned the object to a variable named my_map, now you will be able to handle it as:
my_map.1.title
my_map.3.text
...
to iterate over it just use:
for (info in my_map){
data = my_map[info];
//do what you need
}
For converting JSON object to JSON string use
JSON.stringify(json_object)
For reverse use:
JSON.parse(json_string)
This is the correct way -
final JSONArray arr = new JSONArray();
for(int i = 0 ; i< list.size() ; i++) {
final JSONObject obj = new JSONObject();
p = list.get(i);
obj.add("id", p.getId());
obj.add("title", p.getTitle());
obj.add("date", new MyDateFormatter().getStringFromDateDifference(p.getCreationDate()));
obj.add("txt", getTrimmedText(p.getText()));
arr.add(obj);
}
This will generate
[{"id": 1, "date": 222, "title": "abc", "txt": "some text"}, {...}]
Now, when you parse the json at client end, you can iterate over the array and for each json object you can access as -
obj.id or obj["id"]
I am facing issue in looping in the following code,i am passing a list of forms to it,i am not sure what is going wrong,i need the output as but i am getting only the last form_id.I have this code to get this output as ,here i am passing a list of forms and getting the json as output. Please let me know where am i going wrong.
output :
{
"forms": [
{ "form_id": "1", "form_name": "test1" },
{ "form_id": "2", "form_name": "test2" }
]
}
code :
public class MyFormToJSONConverter {
public JSONObject getJsonFromMyFormObject(List<Form> form) {
JSONObject responseDetailsJson = new JSONObject();
JSONArray jsonArray = null;
List<JSONArray> list = new ArrayList<JSONArray>();
System.out.println(form.size());
for (int i = 0; i < form.size(); i++) {
JSONObject formDetailsJson = new JSONObject();
formDetailsJson.put("form_id", form.get(i).getId());
formDetailsJson.put("form_name", form.get(i).getName());
formDetailsJson.put("desc",
form.get(i).getFormDescription());
jsonArray = new JSONArray();
jsonArray.add(formDetailsJson);
list.add(jsonArray);
}
for (JSONArray json : list) {
responseDetailsJson.put("form", json);
}
return responseDetailsJson;
}
Your problem is here:
for (JSONArray json : list) {
responseDetailsJson.put("form", json);
}
will overwrite all of the previous values with the next value (a single JSON object). You want
responseDetailsJson.put("form", list);
You probably should also get rid of this:
jsonArray = new JSONArray();
jsonArray.add(formDetailsJson);
list.add(jsonArray);
That will give you:
{
"forms": [
[{ "form_id": "1", "form_name": "test1" }],
[{ "form_id": "2", "form_name": "test2" }]
]
}
All told, I think you want:
JSONObject responseDetailsJson = new JSONObject();
List<JSONObject> list = new ArrayList<JSONObject>();
System.out.println(form.size());
// List.get will be very inefficient if passed a LinkedList
// instead of an ArrayList.
for (Form formInstance:form) {
JSONObject formDetailsJson = new JSONObject();
formDetailsJson.put("form_id", formInstance.getId());
formDetailsJson.put("form_name", formInstance.getName());
formDetailsJson.put("desc",
formInstance.getFormDescription());
list.add(formDetailsJson);
}
responseDetailsJson.put("form", list);
JSON objects are essentially key/value pairs. In your code you are doing:
for (JSONArray json : list)
{
responseDetailsJson.put("form", json);
}
You're overwriting the same key each time.