如何将JSON数组转换为Java列表。 我正在使用svenson
我试图将相同类型的多个对象转换为Java中的List
。 例如,我的json将是:
{ "Example": [ { "foo": "a1", "bar": "b1", "fubar": "c1" }, { "foo": "a2", "bar": "b2", "fubar": "c2" }, { "foo": "a3", "bar": "b3", "fubar": "c3" } ] }
我上课了:
public class Example { private String foo; private String bar; private String fubar; public Example(){}; public void setFoo(String f){ foo = f; } public void setBar(String b){ bar = b; } public void setFubar(String f){ fubar = f; } ... }
我希望能够将我得到的json字符串转换为Example
对象列表。 我想做这样的事情:
JSONParser parser = new JSONParser(); parser.addTypeHint(".Example[]", Example.class); List result = parser.parse(List.class, json);
这样做我收到一个错误:
Cannot set property Example on class java.util.ArrayList
您无法将此json转换为List
但您可以将其转换为Map
。
看你的json String
:
... "Example": [ { "foo": "a1", "bar": "b1", "fubar": "c1" }, { "foo": "a2", "bar": "b2", "fubar": "c2" }, ... ] }
这里“Example”是键(String),值是Example的List对象。
尝试这个:
parser.addTypeHint("Example[]", Example.class); Map> result1 = parser.parse(Map.class, json); for (Entry> entry : result1.entrySet()) { for (Example example : entry.getValue()) { System.out.println("VALUE :->"+ example.getFoo()); } }
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> result1 = parser.parse(Map.class, json); for (Entry> 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
我通过修改我的JSON来解决它:
[ { "foo": "a1", "bar": "b1", "fubar": "c1" }, { "foo": "a2", "bar": "b2", "fubar": "c2" }, { "foo": "a3", "bar": "b3", "fubar": "c3" } ]
然后我用java代码:
JSONParser parser = new JSONParser(); ArrayList list = parser.parse(ArrayList.class, json); List result = new ArrayList (); for(int i = 0 ; i < list.size() ; i++){ HashMap 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); }
我有一个像这样的jsonstring:
[ {"author":"amahta","bookId":1,"bookName":"name"}, {"author":"amahta2","bookId":2,"bookName":"name2"} ]
并通过以下代码片段将其转换为列表:
List listdata = new ArrayList (); JSONArray jArray = JSONArray.fromObject(booklist); if (jArray != null) { for (int i = 0; i < jArray.size(); i++) { JSONObject obj = JSONObject.fromObject(jArray.get(i)); listdata.add(new BookVO(Long.valueOf(obj.get("bookId")), obj.get("bookName"), obj.get("author"))); } }
我使用net.sf.json-lib jar文件。
String paymentMethod = "[{\"paymentType\":\"google_iap\",\"msisdn\":1486890084928,\"operator\":\"maroc_ma\",\"paymentMethod\":\"maroc_ma\",\"reactivationEnabled\":true }]"; PaymentMethod mop = null; try { mop = mapper.readValue(paymentMethod, PaymentMethod.class); } catch (Exception e) { logger.warn("Error while parsing paymentMethod = " + paymentMethod + " \n" + e); } List result = new ArrayList (); result.add(mop); billingInfo.setPaymentMethods(result);