首先申明所需 Jar 包 :
一、创建一个实体类 emp.
Paket com.hyx.Entity; public class emp {private Integer id; privater Zeichenfolge Name; Private Ganzzahl dptno; privates String -Geschlecht; private Saitenabgabe; public Integer getid () {return id; } public void setId (Integer id) {this.id = id; } public String getName () {return name; } public void setName (String -Name) {this.name = name; } public Integer getDptno () {return dptno; } public void setDptno (Integer dptno) {thttptno = dptno; } public String getGerTer () {Return Gender; } public void setGender (String -Geschlecht) {this.gender = Gender; } public String getDuty () {Return Duty; } public void setDuty (String Duty) {this.duty = duty; }}二、实体类转换为 json
(1)
importieren java.io.ioxception; import net.sf.json.jsonObject; import org.apache.struts2.json.jsonexception; import org.codehehaus.jackson ObjectMapper Mapper = new ObjectMapper (); // Objekt in JSON String String JSONSON = "" konvertieren; try {j 2nter = mapper.writeValueasString (obj); } catch (ioException e) {throw e; } return jsonObject.fromObject (obj) .ToString (); } // 主函数 public static void main (String [] args) {emp emp = new emp (); Emp.Setid (1); Emp.SetName ("张三"); Emp.SetGene ("男"); Emp.SetDptno (001); Emp.SetDuty ("职员"); String JSONSON = ""; try {JSONSON = ObjectTojson (EMP); } catch (jSonexception e) {e.printstacktrace (); } catch (ioException e) {e.printstacktrace (); } System.out.println (JSONSON); }} (2)
import net.sf.json.jsonObject; import com.hyx.entity.emp; public class Achtung {// 主函数 public static void main (String [] args) {emp = new EMP (); Emp.Setid (1); Emp.SetName ("张三"); Emp.SetGene ("男"); Emp.SetDptno (001); Emp.SetDuty ("职员"); JsonObject jsonObject = jsonObject.fromObject (EMP); System.out.println (JSONObject); }}以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。 也希望大家多多支持武林网。