org.json

配置pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.json</groupId>
  4. <artifactId>json</artifactId>
  5. <version>20160810</version>
  6. </dependency>
  7. </dependencies>

put

  1. JSONObject xiaoming = new JSONObject();
  2. xiaoming.put("name","xiaoming");
  3. xiaoming.put("gender","男");
  4. xiaoming.put("age",20);
  5. xiaoming.put("hobby",new String[]{"羽毛球","爬山"});
  6. Object nullObject = null;
  7. xiaoming.put("comment", nullObject);
  8. System.out.println(xiaoming.toString());

json结果

  1. {"gender":"男","name":"xiaoming","age":20,"hobby":["羽毛球","爬山"]}

HashMap

  1. HashMap<String,Object> xiaoming = new HashMap<String, Object>();
  2. xiaoming.put("name","xiaoming");
  3. xiaoming.put("gender","男");
  4. xiaoming.put("age",20);
  5. xiaoming.put("hobby",new String[]{"羽毛球","爬山"});
  6. Object nullObject = null;
  7. xiaoming.put("comment", nullObject);
  8. System.out.println(new JSONObject(xiaoming).toString());

JavaBean

  1. public class Student {
  2. private String name;
  3. private String gender;
  4. private int age;
  5. private String[] hobby;
  6. private String comment;
  7. public String getName() {
  8. return name;
  9. }
  10. public String getGender() {
  11. return gender;
  12. }
  13. public int getAge() {
  14. return age;
  15. }
  16. public String[] getHobby() {
  17. return hobby;
  18. }
  19. public String getComment() {
  20. return comment;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public void setGender(String gender) {
  26. this.gender = gender;
  27. }
  28. public void setAge(int age) {
  29. this.age = age;
  30. }
  31. public void setHobby(String[] hobby) {
  32. this.hobby = hobby;
  33. }
  34. public void setComment(String comment) {
  35. this.comment = comment;
  36. }
  37. public String toString() {
  38. return "Student{" +
  39. "name='" + name + '\'' +
  40. ", gender='" + gender + '\'' +
  41. ", age=" + age +
  42. ", hobby=" + Arrays.toString(hobby) +
  43. ", comment='" + comment + '\'' +
  44. ", ingorVal='" + ingorVal + '\'' +
  45. '}';
  46. }
  47. }
  1. Student xiaoming = new Student();
  2. xiaoming.setName("xiaoming");
  3. xiaoming.setGender("男");
  4. xiaoming.setAge(20);
  5. xiaoming.setHobby(new String[]{"羽毛球","爬山"});
  6. xiaoming.setComment(null);
  7. System.out.println(new JSONObject(xiaoming).toString());

解析JSON

  1. String jsonStr = "{\"gender\":\"男\",\"name\":\"xiaoming\",\"age\":20,\"hobby\":[\"羽毛球\",\"爬山\"]}";
  2. JSONObject jsonContent = new JSONObject(jsonStr);
  3. System.out.println("name="+jsonContent.getString("name"));
  4. System.out.println("age="+jsonContent.getInt("age"));
  5. System.out.println("hobby="+jsonContent.getJSONArray("hobby").getString(0));

gson

  1. <dependency>
  2. <groupId>com.google.code.gson</groupId>
  3. <artifactId>gson</artifactId>
  4. <version>2.8.5</version>
  5. </dependency>
  1. Student xiaoming = new Student();
  2. xiaoming.setName("xiaoming");
  3. xiaoming.setGender("男");
  4. xiaoming.setAge(20);
  5. xiaoming.setHobby(new String[]{"羽毛球","爬山"});
  6. xiaoming.setComment(null);
  7. System.out.println(new Gson().toJson(xiaoming));

注解 @SerializedName

  1. @SerializedName("Name")
  2. private String name;
  3. @SerializedName("Gender")
  4. private String gender;
  5. @SerializedName("Age")
  6. private int age;
  7. @SerializedName("Hobby")
  8. private String[] hobby;
  9. @SerializedName("Comment")
  10. private String comment;
  1. GsonBuilder builder = new GsonBuilder();
  2. builder.setFieldNamingStrategy(new FieldNamingStrategy() {
  3. public String translateName(Field field) {
  4. String name = field.getName();
  5. char [] cs = name.toCharArray();
  6. cs[0]-=32;
  7. return String.valueOf(cs);
  8. }
  9. });
  10. System.out.println(builder.create().toJson(xiaoming));

transient 忽略某个字段

  1. private transient String ingorVal;

解析json

  1. String jsonStr = "{\"gender\":\"男\",\"name\":\"xiaoming\",\"age\":20,\"hobby\":[\"羽毛球\",\"爬山\"]}";
  2. Gson gson = new Gson();
  3. Student s = gson.fromJson(jsonStr,Student.class);
  4. System.out.println(s);