org.json

来源:互联网 发布:php httpclient 类库 编辑:程序博客网 时间:2024/04/26 22:00
package com.betasoft.util;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.json.JSONObject;import org.junit.Test;import com.betasoft.entity.User;public class JsonParse {@Testpublic void jsonObjectMethod(){JSONObject user = new JSONObject();Object nullObj = null;user.put("name","王小二");user.put("age",25.2);user.put("birthday","1991-11-28");user.put("school","蓝翔");user.put("hasGirlfriend",false);user.put("major",new String[]{"理发","挖掘机"});user.put("car",nullObj);System.out.println(user.toString());}@Testpublic void jsonObjectByMap(){Map<String,Object> user = new HashMap<>();Object nullObj = null;user.put("name","王小二");user.put("age",25.2);user.put("birthday","1991-11-28");user.put("school","蓝翔");user.put("hasGirlfriend",false);user.put("major",new String[]{"理发","挖掘机"});user.put("car",nullObj);System.out.println(new JSONObject(user).toString());}@Testpublic void jsonObjectByBean(){User user = new User();user.setName("王小二");user.setAge(25.2);user.setBirthday(new Date());user.setSchool("蓝翔");user.setHasGirlfriend(false);user.setMajor(new String[]{"理发","挖掘机"});user.setCar(null);System.out.println(new JSONObject(user));}}
package com.betasoft.util;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.json.JSONArray;import org.json.JSONObject;public class JsonRead {public static void main(String[] args) {File file = new File("src/file/wangxiaoer.json");try {String content = FileUtils.readFileToString(file);JSONObject jsonObject = new JSONObject(content);if(!jsonObject.isNull("name")){System.out.println("姓名是:"+jsonObject.getString("name"));}System.out.println("年龄是:"+jsonObject.getDouble("age"));System.out.println("是否有女朋友:"+jsonObject.getBoolean("hasGirlfriend"));JSONArray  array = jsonObject.getJSONArray("major");for(int i = 0; i < array.length() ; i++){String major = (String)array.get(i);System.out.println("专业:"+major);}} catch (IOException e) {e.printStackTrace();}}}

package com.betasoft.entity;import java.util.Arrays;import java.util.Date;import com.google.gson.annotations.SerializedName;public class User {//@SerializedName("NAME")private String name;private double age;private Date birthday;private String school;private boolean hasGirlfriend;private String[] major;private Object car;private transient String ignore;public String getName() {return name;}public void setName(String name) {this.name = name;}public double getAge() {return age;}public void setAge(double age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getSchool() {return school;}public void setSchool(String school) {this.school = school;}public boolean isHasGirlfriend() {return hasGirlfriend;}public void setHasGirlfriend(boolean hasGirlfriend) {this.hasGirlfriend = hasGirlfriend;}public String[] getMajor() {return major;}public void setMajor(String[] major) {this.major = major;}public Object getCar() {return car;}public void setCar(Object car) {this.car = car;}public String getIgnore() {return ignore;}public void setIgnore(String ignore) {this.ignore = ignore;}@Overridepublic String toString() {return "User [name=" + name + ", age=" + age + ", birthday=" + birthday+ ", school=" + school + ", hasGirlfriend=" + hasGirlfriend+ ", major=" + Arrays.toString(major) + ", car=" + car + "]";}}


{"hasGirlfriend":false,"birthday":"1991-11-28","school":"蓝翔","name":"王小二","age":25.2,"major":["理发","挖掘机"]}



 
原创粉丝点击