JAVA写JSON的三种方法,java对象转json数据
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
今天给大家讲一个对象,对象集合转json数据的三种方法,三种方法最终达到的效果是一样的。
1,jsonlib:个人感觉最麻烦的一个需要引入的包也多,代码也相对多一些。
2,Gson:谷歌的
3,FastJson:阿里巴巴的,个人觉得这个比较好,而且据说这个也是性能最好的一个。
关于demo里面所使用的罐包,可以自行去下载,也可以加我QQ我给你:1140459171
Jsonlib:package json;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import user.User;
public class Jsonlib {
public static void main(String[] args) {
User user1 = new User();
user1.setUsername("111");
user1.setPassword("111");
User user2 = new User();
user2.setUsername("222");
user2.setPassword("222");
List<User> lists = new ArrayList<User>();
lists.add(user1);
lists.add(user2);
JSONArray fromObject1 = JSONArray.fromObject(user1);
JSONArray fromObject2 = JSONArray.fromObject(lists);
String str1 = fromObject1.toString();
String str2 = fromObject2.toString();
System.out.println(str1);
//打印结果
//[{"password":"111","username":"111"}]
System.out.println(str2);
//打印结果
//[{"password":"111","username":"111"},{"password":"222","username":"222"}]
}
}
jar:
Gson:
package json;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import user.User;
public class Gson_a {
public static void main(String[] args) {
User user1 = new User();
user1.setUsername("111");
user1.setPassword("111");
User user2 = new User();
user2.setUsername("222");
user2.setPassword("222");
List<User> lists = new ArrayList<User>();
lists.add(user1);
lists.add(user2);
Gson gson = new Gson();
String json1 = gson.toJson(user1);
System.out.println(json1);
//打印结果
//{"username":"111","password":"111"}
String json2 = gson.toJson(lists);
System.out.println(json2);
//打印结果
//[{"username":"111","password":"111"},{"username":"222","password":"222"}]
}
}
jar:
FastJson:
package json;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import user.User;
public class FastJson {
public static void main(String[] args) {
User user1 = new User();
user1.setUsername("111");
user1.setPassword("111");
//1、单个对象转换成json
String jsonString = JSON.toJSONString(user1);
System.out.println(jsonString);
//打印结果
//{"password":"111","username":"111"}
User user2 = new User();
user2.setUsername("222");
user2.setPassword("222");
List<User> lists = new ArrayList<User>();
lists.add(user1);
lists.add(user2);
String jsonString2 = JSON.toJSONString(lists);
System.out.println(jsonString2);
//打印结果
//[{"password":"111","username":"111"},{"password":"222","username":"222"}]
}
}
所有文章优先发布在个人博客,后面修改可能忘记同步到CSDN,给你带来不辨表示抱歉。
个人博客本篇文章 : https://www.xdx97.com/article?bamId=656117636463067136
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> 2 天前
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> 2 天前
更多推荐
已为社区贡献12条内容
所有评论(0)