久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

如何將JSON數(shù)組轉(zhuǎn)換為Java列表。我正在使用svenson

瀏覽:135日期:2024-05-02 18:48:57
如何解決如何將JSON數(shù)組轉(zhuǎn)換為Java列表。我正在使用svenson?

您不能將此json轉(zhuǎn)換為,List但可以將其轉(zhuǎn)換為Map。看到你的json String:

...'Example': [{ 'foo': 'a1', 'bar': 'b1', 'fubar': 'c1'},{ 'foo': 'a2', 'bar': 'b2', 'fubar': 'c2'},...]}

試試這個(gè):

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

的完整代碼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; }}

VALUE :->a1VALUE :->a2VALUE :->a3解決方法

我試圖將多個(gè)相同類型的對象轉(zhuǎn)換為ListJava。例如,我的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; }...}

我希望能夠?qū)@取的json字符串轉(zhuǎn)換為Example對象列表。我想做這樣的事情:

JSONParser parser = new JSONParser();parser.addTypeHint('.Example[]',Example.class);List<Example> result = parser.parse(List.class,json);

這樣做我得到一個(gè)錯(cuò)誤:

Cannot set property Example on class java.util.ArrayList

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 平顺县| 海原县| 襄垣县| 从江县| 宜都市| 高陵县| 久治县| 四川省| 莆田市| 大关县| 房山区| 盖州市| 阜宁县| 都匀市| 蛟河市| 富蕴县| 定襄县| 六枝特区| 安义县| 任丘市| 武山县| 永康市| 澄迈县| 恩施市| 渝北区| 宁陵县| 探索| 贺兰县| 台东县| 鹤峰县| 杭锦旗| 图们市| 保靖县| 调兵山市| 东至县| 和硕县| 古田县| 天津市| 四子王旗| 娄底市| 阳原县|