现有一串,从数据库中读取的JSON字符串,如下: String techname; public String getTechname() { public String getOutarea() { } |
|
18分 |
有几行用了java 1.8 ,其他的都可以的,
解析json需要导入的包,可以参考:http://download.csdn.net/detail/yang12hua34/4062529 import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.util.ArrayList; import java.util.List; /** * Created by puruidong on 2015/5/1. */ class MktorderInfo { String techname; String area; String outarea; public String getTechname() { return techname; } public void setTechname(String techname) { this.techname = techname; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getOutarea() { return outarea; } public void setOutarea(String outarea) { this.outarea = outarea; } /** * 创建一个T对象. * * @param jo * @return */ public static MktorderInfo createMktorderInfo(JSONObject jo) { if (jo == null || jo.size() == 0) { return null; } MktorderInfo mi = new MktorderInfo(); mi.setArea(jo.containsKey("area") ? jo.getString("area") : ""); mi.setOutarea(jo.containsKey("outarea") ? jo.getString("outarea") : ""); mi.setTechname(jo.containsKey("techname") ? jo.getString("techname") : ""); return mi; } /** * 创建一个List<T> * * @param array * @return */ public static List<MktorderInfo> createMktorderInfoList(JSONArray array) { if (array.size() == 0) { return null; } List<MktorderInfo> list = new ArrayList<MktorderInfo>(); for (int i = 0; i < array.size(); i++) { JSONObject jo = array.getJSONObject(i); list.add(createMktorderInfo(jo)); } return list; } } public class Entity { public static void main(String[] args) { String str = "[{""area"":""0.42"",""outarea"":""0.00"",""techname"":""OSP""},{""area"":""0.00"",""outarea"":""191.10"",""techname"":""VCUT""}]"; JSONArray ja = JSONArray.fromObject(str); List<MktorderInfo> mlist = MktorderInfo.createMktorderInfoList(ja); //下面是java 1.8 代码. mlist.forEach(t -> { System.out.println("T*>>>>>>" + t + ">>>>area:" + t.getArea() + ",outarea:" + t.getOutarea() + ",techname:" + t.getTechname()); }); } } |
1分 |
T*>>>>>>MktorderInfo@3d012ddd>>>>area:0.42,outarea:0.00,techname:OSP
T*>>>>>>MktorderInfo@6f2b958e>>>>area:0.00,outarea:191.10,techname:VCUT Process finished with exit code 0 上面程序的运行结果. |