1.后台接收json数组转成封装实体类的List:
package no.integrasco.ingentia.news.qaedition; public class Person {</span><span style="color: #0000ff">private</span><span style="color: #000000"> String name; </span><span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span><span style="color: #000000"> age; </span><span style="color: #0000ff">public</span><span style="color: #000000"> String getName() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> name; } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setName(String name) { </span><span style="color: #0000ff">this</span>.name =<span style="color: #000000"> name; } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span><span style="color: #000000"> getAge() { </span><span style="color: #0000ff">return</span><span style="color: #000000"> age; } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setAge(<span style="color: #0000ff">int</span><span style="color: #000000"> age) { </span><span style="color: #0000ff">this</span>.age =<span style="color: #000000"> age; }
}
package no.integrasco.ingentia.news.qaedition; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JsonConfig; public class JsonTest {</span><span style="color: #008000">/**</span><span style="color: #008000"> * </span><span style="color: #808080">@param</span><span style="color: #008000"> args </span><span style="color: #008000">*/</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] args) { </span><span style="color: #008000">//</span><span style="color: #008000"> 转换方法1</span> JSONArray array = JSONArray.fromObject("[{'name':'hehe','age':22}]"<span style="color: #000000">); List</span><Person> list = JSONArray.toList(array, Person.<span style="color: #0000ff">class</span>);<span style="color: #008000">//</span><span style="color: #008000"> 过时方法</span> System.out.println(list.get(0<span style="color: #000000">).getName()); </span><span style="color: #008000">//</span><span style="color: #008000"> 转换方法2</span> List<?> list2 = JSONArray.toList(array, <span style="color: #0000ff">new</span> Person(), <span style="color: #0000ff">new</span> JsonConfig());<span style="color: #008000">//</span><span style="color: #008000">参数1为要转换的JSONArray数据,参数2为要转换的目标数据,即List盛装的数据</span> Person person = (Person) list2.get(0<span style="color: #000000">); System.out.println(person.getAge()); }
}
2.从页面接收json格式的数据,在java类里面进行解析
String jsonStr = {"name":"zfj","dd":\[{"ddr":"1","encrypt":"2","ff":"1","length":"23","ffe":"editStyled","ill":"1","pkor":"2","name":"zfj","isKey":"2","alias":"ffff"}\],"addRelations":\[{"type":"2","ld":"zfj","ld":"2"}\]}; //将此字符串放在ruquest或者其他的变量里面,传递到后台。 //如放在reques中: request.getRequestDispatcher("/geServlet?data="+jsonStr).forward(request,response); //在servlet或java类里这样接收: String json = request.ge[size=large][/size]tParameter("data"); JSONObject jsonObject = JSONObject.fromObject(data); String name = jsonObject.getString(“name”); ...... //对于数组这样接收用 JSONArray jan = (JSONArray) jsonObject.get("dd"); if(jan!=null||jan.size()!=0){ for(int i=0;i<ja.size();i++){ JSONObject jo = JSONObject.fromObject(ja.get(i)); String ff = jo.getString("ff"); ...... } }
String jsonStr = {"name":"zfj","dd":\[{"ddr":"1","encrypt":"2","ff":"1","length":"23","ffe":"editStyled","ill":"1","pkor":"2","name":"zfj","isKey":"2","alias":"ffff"}\],"addRelations":\[{"type":"2","ld":"zfj","ld":"2"}\]}; //将此字符串放在ruquest或者其他的变量里面,传递到后台。 //如放在reques中: request.getRequestDispatcher("/geServlet?data="+jsonStr).forward(request,response); //在servlet或java类里这样接收: String json = request.getParameter("data"); JSONObject jsonObject = JSONObject.fromObject(data); String name = jsonObject.getString(“name”); ...... //对于数组这样接收用 JSONArray jan = (JSONArray) jsonObject.get("dd"); if(jan!=null||jan.size()!=0){ for(int i=0;i<ja.size();i++){ JSONObject jo = JSONObject.fromObject(ja.get(i)); String ff = jo.getString("ff"); ...... } }