将JSon格式的"数组"字符串转换为List集合。
应用此技术从一个json对象字符串格式中得到一个java对应的对象。
JSONObject是一个“name.values”集合,
通过get(key)方法取得key对应的value部分(字符串)。
通过getJSONObject(key)可以取得一个JSONObject对象。
通过getJSONArray(key)可以得到一个JSONArray对象。
导入jar包:
编写:po(bean)类:
package com.west.webcourse.po; /** * 第01步:编写bean类, * 下一步com.west.webcourse.servlet.JavaBeanToJOSNString.java */ public class PersonInfoPo { private String name; private int age; private String sex; public PersonInfoPo(){} public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
第02步:编写servlet类:
package com.west.webcourse.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.west.webcourse.po.PersonInfoPo; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JavaBeanToJOSNString extends HttpServlet { PersonInfoPo pif = new PersonInfoPo(); PersonInfoPo pif01 = new PersonInfoPo(); /** 第03步:重写doGet()方法,下一步:测试 */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); System.out.println("转换"); out.print("转换后的JSON字符串:<br/>"); /** 向浏览器发送JSon格式的字符串 */ getJsonStringToBeanList(); out.flush(); out.close(); } /** 第02步:将JSon类型字符串转换为:List集合对象 */ public List<PersonInfoPo> getJsonStringToBeanList() { String jsonString = "[{\"name\":\"zhang3\",\"sex\":\"nan\",\"age\":\"30\"},{\"name\":\"zhang4\",\"sex\":\"nan4\",\"age\":\"304\"}]"; System.out.println(jsonString); /* 2.1:转换成json数组 */ try { /* 2.2:创建PersonInfoPo对象的List列表 */ List<PersonInfoPo> lp = new ArrayList<PersonInfoPo>(); /**2.3:Json字符串,转换为JsonArray数组*/ JSONArray PoJsonArray = JSONArray.fromObject(jsonString); /**2.4:遍历数组,取出JsonObject对象,并转换成PersonInfoPo对象,添加到List列表*/ for (Object object : PoJsonArray) { PersonInfoPo pif02 = new PersonInfoPo(); /**2.4.1:将JsonObject对象转换成PersonInfoPo对象*/ pif02=(PersonInfoPo)JSONObject.toBean((JSONObject)object,PersonInfoPo.class); lp.add(pif02); System.out.println("装入:"+pif02.getName()); } return lp; } catch (Exception e) { e.printStackTrace(); } return null; } }
测试servlet:
package com.west.webcourse.servlet; /** * 测试 */ import java.util.List; import org.junit.BeforeClass; import org.junit.Test; import com.west.webcourse.po.PersonInfoPo; public class JavaBeanToJOSNStringTest { static JavaBeanToJOSNString js; @BeforeClass public static void setUpBeforeClass() throws Exception { js=new JavaBeanToJOSNString(); } @Test public void testGetJsonStringToBeanList() { List<PersonInfoPo> lp=js.getJsonStringToBeanList(); for (PersonInfoPo person : lp) { System.out.println("测试,循环取出:"+person.getName()); } } }
时间: 2024-10-12 20:31:03