简单实现_实体类与Json字符串互相转换
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
文章目录
一、Jackson方式
1、实体类
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2、实体类转为Json
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
System.out.println("=============实体类转为Json=============");
Person person = new Person();
person.setAge(15);
person.setName("小明");
String result = mapper.writeValueAsString(person);
System.out.println(result);
System.out.println("=============Json转为实体类=============");
String str = "{\"name\":\"小明\",\"age\":15}";
Person person1 = mapper.readValue(str, Person.class);
System.out.println("姓名为:" + person1.getName() + ",年龄为:" + person1.getAge());
}
}
3、运行结果
4、Spring boot中使用
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
public class Test {
@Autowired
private ObjectMapper mapper;
public void test() throws JsonProcessingException {
System.out.println("=============实体类转为Json=============");
Person person = new Person();
person.setAge(15);
person.setName("小明");
String result = mapper.writeValueAsString(person);
System.out.println(result);
System.out.println("=============Json转为实体类=============");
String str = "{\"name\":\"小明\",\"age\":15}";
Person person1 = mapper.readValue(str, Person.class);
System.out.println("姓名为:" + person1.getName() + ",年龄为:" + person1.getAge());
}
}
二、FastJson方式
1)、案例
1、Pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
2、实体类
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
3、实体类与Json转换
import com.alibaba.fastjson.JSON;
public class Test {
public static void main(String[] args) {
System.out.println("=============实体类转为Json=============");
Person person = new Person();
person.setAge(15);
person.setName("小明");
String result = JSON.toJSONString(person);
System.out.println(result);
System.out.println("=============Json转为实体类=============");
String str = "{\"name\":\"小明\",\"age\":15}";
Person person1 = JSON.parseObject(str, Person.class);
System.out.println("姓名为:" + person1.getName() + ",年龄为:" + person1.getAge());
}
}
4、运行结果
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 3 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 3 天前
更多推荐
已为社区贡献19条内容
所有评论(0)