Java对象以User.class为例
,注意:代码中使用到了lombok的@Data注解
package com.example.entity;
import lombok.Data;
@Data
public class User {
// 姓名
private String name;
// 年龄
private int age;
}
<!--json-lib-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
package com.example.demo;
import com.example.entity.User;
import net.sf.json.JSONObject;
public class TestA {
public static void main(String[] args) throws Exception {
String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}";
//1、使用JSONObject
JSONObject jsonObject = JSONObject.fromObject(objectStr);
User user = (User) JSONObject.toBean(jsonObject, User.class);
System.out.println("name:" + user.getName());
System.out.println("age:" + user.getAge());
}
}
package com.example.demo;
import com.example.entity.User;
import net.sf.json.JSONObject;
public class TestA {
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("张三");
user.setAge(18);
// 1.对象转JSONObject
JSONObject json = JSONObject.fromObject(user);
// 2.JSONObject转String
String strJson = json.toString();
System.out.println(strJson);
}
}
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
package com.example.demo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.User;
public class TestA {
public static void main(String[] args) throws Exception {
String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}";
JSONObject jsonObject = JSON.parseObject(objectStr);
User user = JSON.toJavaObject(jsonObject, User.class);
System.out.println("name:" + user.getName());
System.out.println("age:" + user.getAge());
}
}
package com.example.demo;
import com.alibaba.fastjson.JSON;
import com.example.entity.User;
public class TestA {
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("张三");
user.setAge(18);
String str = JSON.toJSONString(user);
System.out.println(str);
}
}
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.7.1</version>
</dependency>
package com.example.demo;
import com.example.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestA {
public static void main(String[] args) throws Exception {
String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}";
ObjectMapper om = new ObjectMapper();
User user = om.readValue(objectStr, User.class);
System.out.println("name:" + user.getName());
System.out.println("age:" + user.getAge());
}
}
package com.example.demo;
import com.example.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestA {
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("张三");
user.setAge(18);
ObjectMapper om = new ObjectMapper();
String str = om.writeValueAsString(user);
System.out.println(str);
}
}
<!-- 谷歌依赖 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
package com.example.demo;
import com.example.entity.User;
import com.google.gson.Gson;
public class TestA {
public static void main(String[] args) throws Exception {
String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}";
Gson gson = new Gson();
User user = gson.fromJson(objectStr, User.class);
System.out.println("name:" + user.getName());
System.out.println("age:" + user.getAge());
}
}
package com.example.demo;
import com.example.entity.User;
import com.google.gson.Gson;
public class TestA {
public static void main(String[] args) throws Exception {
User user = new User();
user.setName("张三");
user.setAge(18);
Gson gson = new Gson();
String str = gson.toJson(user);
System.out.println(str);
}
}
以上就是常用的几种String转具体的java对象操作