You cannot convert this json to List but you can convert this to Map.
See your json String:
...
"Example": [
{
"foo": "a1",
"bar": "b1",
"fubar": "c1"
},
{
"foo": "a2",
"bar": "b2",
"fubar": "c2"
},
...
]
}
Here "Example" is key(String) and value is List object of Example.
Try this:
parser.addTypeHint("Example[]", Example.class);
Map<String,List<Example>> result1 = parser.parse(Map.class, json);
for (Entry<String, List<Example>> entry : result1.entrySet()) {
for (Example example : entry.getValue()) {
System.out.println("VALUE :->"+ example.getFoo());
}
}
Full code of Example:
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.svenson.JSONParser;
public class Test {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
parser.addTypeHint(".Example[]", Example.class);
String json = "{" + "\"Example\": [" + "{" + "\"foo\": \"a1\","
+ "\"bar\": \"b1\"," + "\"fubar\": \"c1\"" + "}," + "{"
+ "\"foo\": \"a2\"," + "\"bar\": \"b2\"," + "\"fubar\": \"c2\""
+ "}," + "{" + "\"foo\": \"a3\"," + "\"bar\": \"b3\","
+ "\"fubar\": \"c3\"" + "}" + "]" + "}\"";
parser.addTypeHint("Example[]", Example.class);
Map<String, List<Example>> result1 = parser.parse(Map.class, json);
for (Entry<String, List<Example>> entry : result1.entrySet()) {
for (Example example : entry.getValue()) {
System.out.println("VALUE :->" + example.getFoo());
}
}
}
}
public class Example {
private String foo;
private String bar;
private String fubar;
public Example(){}
public void setFoo(String foo) {
this.foo = foo;
}
public String getFoo() {
return foo;
}
public void setBar(String bar) {
this.bar = bar;
}
public String getBar() {
return bar;
}
public void setFubar(String fubar) {
this.fubar = fubar;
}
public String getFubar() {
return fubar;
}
}
OutPut:
VALUE :->a1
VALUE :->a2
VALUE :->a3
Answer from Subhrajyoti Majumder on Stack OverflowVideos
You cannot convert this json to List but you can convert this to Map.
See your json String:
...
"Example": [
{
"foo": "a1",
"bar": "b1",
"fubar": "c1"
},
{
"foo": "a2",
"bar": "b2",
"fubar": "c2"
},
...
]
}
Here "Example" is key(String) and value is List object of Example.
Try this:
parser.addTypeHint("Example[]", Example.class);
Map<String,List<Example>> result1 = parser.parse(Map.class, json);
for (Entry<String, List<Example>> entry : result1.entrySet()) {
for (Example example : entry.getValue()) {
System.out.println("VALUE :->"+ example.getFoo());
}
}
Full code of Example:
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.svenson.JSONParser;
public class Test {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
parser.addTypeHint(".Example[]", Example.class);
String json = "{" + "\"Example\": [" + "{" + "\"foo\": \"a1\","
+ "\"bar\": \"b1\"," + "\"fubar\": \"c1\"" + "}," + "{"
+ "\"foo\": \"a2\"," + "\"bar\": \"b2\"," + "\"fubar\": \"c2\""
+ "}," + "{" + "\"foo\": \"a3\"," + "\"bar\": \"b3\","
+ "\"fubar\": \"c3\"" + "}" + "]" + "}\"";
parser.addTypeHint("Example[]", Example.class);
Map<String, List<Example>> result1 = parser.parse(Map.class, json);
for (Entry<String, List<Example>> entry : result1.entrySet()) {
for (Example example : entry.getValue()) {
System.out.println("VALUE :->" + example.getFoo());
}
}
}
}
public class Example {
private String foo;
private String bar;
private String fubar;
public Example(){}
public void setFoo(String foo) {
this.foo = foo;
}
public String getFoo() {
return foo;
}
public void setBar(String bar) {
this.bar = bar;
}
public String getBar() {
return bar;
}
public void setFubar(String fubar) {
this.fubar = fubar;
}
public String getFubar() {
return fubar;
}
}
OutPut:
VALUE :->a1
VALUE :->a2
VALUE :->a3
I solved it by modifying my JSON to be in the form:
[
{
"foo": "a1",
"bar": "b1",
"fubar": "c1"
},
{
"foo": "a2",
"bar": "b2",
"fubar": "c2"
},
{
"foo": "a3",
"bar": "b3",
"fubar": "c3"
}
]
Then I used the java code:
JSONParser parser = new JSONParser();
ArrayList list = parser.parse(ArrayList.class, json);
List<Example> result = new ArrayList<Example>();
for(int i = 0 ; i < list.size() ; i++){
HashMap<String, String> map = (HashMap) list.get(i);
Example example = new Example();
example.setFoo(map.get("foo"));
example.setBar(map.get("bar"));
example.setFubar(map.get("fubar"));
result.add(example);
}
Use GSON library for that. Here is the sample code
List<String> foo = new ArrayList<String>();
foo.add("A");
foo.add("B");
foo.add("C");
String json = new Gson().toJson(foo );
Here is the maven dependency for Gson
<dependencies>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
Or you can directly download jar from here and put it in your class path
http://code.google.com/p/google-gson/downloads/detail?name=gson-1.0.jar&can=4&q=
To send Json to client you can use spring or in simple servlet add this code
response.getWriter().write(json);
You need an external library for this.
JSONArray jsonA = JSONArray.fromObject(mybeanList);
System.out.println(jsonA);
Google GSON is one of such libraries
You can also take a look here for examples on converting Java object collection to JSON string.
The second is almost correct:
Copy{
"foos" : [{
"prop1":"value1",
"prop2":"value2"
}, {
"prop1":"value3",
"prop2":"value4"
}]
}
The first example from your question,
Copyfoos: [
foo: { ... },
foo: { ... }
]
is in invalid syntax. You cannot have object properties inside a plain array.
The second example from your question,
Copyfoos: [
{ ... },
{ ... }
]
is right although it is not strict JSON. It's a relaxed form of JSON wherein quotes in string keys are omitted.
Following is the correct one when you want to obey strict JSON:
Copy"foos": [
{ ... },
{ ... }
]
This "Mastering JSON" tutorial by Patrick Hunlock, may help to learn about JSON and this site may help to validate JSON.
You are asking Jackson to parse a StudentList. Tell it to parse a List (of students) instead. Since List is generic you will typically use a TypeReference
List<Student> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Student>>(){});
For any one who looks for answer yet:
1.Add jackson-databind library to your build tools like Gradle or Maven
2.in your Code:
ObjectMapper mapper = new ObjectMapper();
List<Student> studentList = new ArrayList<>();
studentList = Arrays.asList(mapper.readValue(jsonStringArray, Student[].class));
Your root JSON is an Array, so first create a JSONArray from your String.
Do this:
JSONArray arr = new JSONArray(jstring);
for (int i = 0; i < arr.length(); i++) { // Walk through the Array.
JSONObject obj = arr.getJSONObject(i);
JSONArray arr2 = obj.getJSONArray("fileName");
// Do whatever.
}
For more info, please refer to the docs on JSONArray and JSONObject.
You have to directly construct JSONArray from JSON string in this case.
JSONArray arr = new JSONArray(jstring);