webservice json里传的是list 如何将json中的值转换成实体对象? 5
webserviceObject[]result=client.invoke("getPersons",newObject[]{"xxx","xxx",param});打...
webservice
Object[] result=client.invoke("getPersons", new Object[]{"xxx","xxx",param});
打印获取到的result[0] 值为:
[ { "city" : "XX市", "zt" : "0", "xm" : "王方","sfzh" : "211319195410267311"},
{ "city" : "XX市", "zt" : "0", "xm" : "王英","sfzh" : "211319195205287521"},
{ "city" : "XX市", "zt" : "0", "xm" : "王花","sfzh" : "211319193601061421"} ]
请问怎么将json值转换成实体对象 展开
Object[] result=client.invoke("getPersons", new Object[]{"xxx","xxx",param});
打印获取到的result[0] 值为:
[ { "city" : "XX市", "zt" : "0", "xm" : "王方","sfzh" : "211319195410267311"},
{ "city" : "XX市", "zt" : "0", "xm" : "王英","sfzh" : "211319195205287521"},
{ "city" : "XX市", "zt" : "0", "xm" : "王花","sfzh" : "211319193601061421"} ]
请问怎么将json值转换成实体对象 展开
1个回答
展开全部
您好,
package com.nubb.test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.nubb.bean.Person;
public class JSONSerializer {
private static final String DEFAULT_CHARSET_NAME = "UTF-8";
public static <T> String serialize(T object) {
return JSON.toJSONString(object);
}
public static <T> T deserialize(String string, Class<T> clz) {
return JSON.parseObject(string, clz);
}
public static <T> T load(Path path, Class<T> clz) throws IOException {
return deserialize(
new String(Files.readAllBytes(path), DEFAULT_CHARSET_NAME), clz);
}
public static <T> void save(Path path, T object) throws IOException {
if (Files.notExists(path.getParent())) {
Files.createDirectories(path.getParent());
}
Files.write(path,
serialize(object).getBytes(DEFAULT_CHARSET_NAME),
StandardOpenOption.WRITE,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
}
public static void main(String[] args) {
Person person1 = new Person();
person1.setAddress("address");
person1.setAge(11);
person1.setName("amao");
Person person2 = new Person();
person2.setAddress("address");
person2.setAge(11);
person2.setName("amao");
List<Person> lp = new ArrayList<Person>();
lp.add(person1);
lp.add(person2);
System.out.println(serialize(lp));
}
}
package com.nubb.test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.nubb.bean.Person;
public class JSONSerializer {
private static final String DEFAULT_CHARSET_NAME = "UTF-8";
public static <T> String serialize(T object) {
return JSON.toJSONString(object);
}
public static <T> T deserialize(String string, Class<T> clz) {
return JSON.parseObject(string, clz);
}
public static <T> T load(Path path, Class<T> clz) throws IOException {
return deserialize(
new String(Files.readAllBytes(path), DEFAULT_CHARSET_NAME), clz);
}
public static <T> void save(Path path, T object) throws IOException {
if (Files.notExists(path.getParent())) {
Files.createDirectories(path.getParent());
}
Files.write(path,
serialize(object).getBytes(DEFAULT_CHARSET_NAME),
StandardOpenOption.WRITE,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
}
public static void main(String[] args) {
Person person1 = new Person();
person1.setAddress("address");
person1.setAge(11);
person1.setName("amao");
Person person2 = new Person();
person2.setAddress("address");
person2.setAge(11);
person2.setName("amao");
List<Person> lp = new ArrayList<Person>();
lp.add(person1);
lp.add(person2);
System.out.println(serialize(lp));
}
}
追问
兄弟,我这是webservice返回的json,用Object[]接的json值
想把返回值中的几组数据一一转换成实体对象,不是把实体转换成json
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询