java json字符串和对象互转
/**
* Created by admin on 2017/7/26.
*/
public class NewPost {
private String title;
private String content;
public NewPost(){
}
public NewPost(String title,String content){
setTitle(title);
setContent(content);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent(){
return content;
}
public void setContent(String content){
this.content=content;
}
@Override
public String toString() {
return "NewPost{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
import com.alibaba.fastjson.JSON; import java.util.ArrayList;
import java.util.List; /**
* Created by admin on 2017/7/26.
*/
class TestFastjson {
String json1="[{\"title\":\"post1\",\"content\":\"post1\"},{\"title\":\"post1\",\"content\":\"post1\"}]";
String json2="{\"title\":\"post1\",\"content\":\"post1\"}"; public void testFJ(){
//json字符串转list对象
// List<NewPost> list= new ArrayList<NewPost>();
List listpost =JSON.parseObject(json1,List.class);
System.out.println("listpost "+listpost);
//json字符串转对象
NewPost post =JSON.parseObject(json2,NewPost.class);
System.out.println("post "+post);
//对象转json字符串
List<NewPost> list= new ArrayList<NewPost>();
list.add(new NewPost("post1","post1"));
list.add(new NewPost("post2","post2"));
String jsonString= JSON.toJSONString(list);
System.out.println("jsonString "+jsonString);
}
public static void main(String[] args) {
TestFastjson tf=new TestFastjson();
tf.testFJ();
}
}
listpost1 [{"title":"post1","content":"post1"}, {"title":"post1","content":"post1"}]
listpost2 NewPost{title='post1', content='post1'}
jsonString [{"content":"post1","title":"post1"},{"content":"post2","title":"post2"}]
java json字符串和对象互转的更多相关文章
- Java——Json字符串与Object互转
public static void JacksonTest() {//推荐 //{"MNG001":[{"ID":"1","PW ...
- jQuery 中json字符串与对象互转
json字符串转json对象:jQuery.parseJSON(jsonStr); json对象转json字符串:JSON.stringify(jsonObj);
- java json字符串与对象转换
下载引入包gson-2.0.jar 1.字符转数据 final Map map = new HashMap();map.put("contents", "[{\&q ...
- Swift开发中 JSON对象/JSON字符串/Data的互转
本文将介绍Swift开发中常用的转换(JSON对象/JSON字符串/Data之间的互相转换) #pragma mark - JSON(对象)----->JSON字符串 1.原生方法 //JSON ...
- Google Gson实现JSON字符串和对象之间相互转换
User实体类 package com.test.json; /** * User 实体类 */ public class User { private String name; private St ...
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
[源码下载] 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 作者:webabcd 介绍速战速决 之 PHP ...
- json字符串和对象的相互转化
json在代码中是经常用到的,在此总结一下json字符串和对象及数组之间的相互转化: 1.javascript函数方式: <1> JSON.stringify :把一个对象转换成json字 ...
- js在前端json字符串和对象互相转化
js在前端json字符串和对象互相转化 //对象转json串 注意:参数必须是对象,数组不起作用,对象格式{'0'=>'a'} JSON.stringify( arr ); //json字符串转 ...
- 小程序json字符串转为对象
小程序里json字符串转为对象使用JSON.parse()方法转变无效, 看报错提示有单引号“ ' ” 因为单引号而无效, 将单引号全改双引号即可. 报错如下: VM11050:1 thirdScri ...
随机推荐
- SpingMVC实现操作的第一方式
(一) 使用SpringMVC框架的步骤 (1):在web.xml中配置前端控制器 (2):在Spring-servlet.xml中配置 配置处理器映射器HandlerMapping(处理器Handl ...
- [转] Matlab与C++混合编程,添加OpenCV库
原文地址 峰回璐转 最近在做运动医学软件优化工作,此款软件框架及算法语言全由matlab实现,虽然matlab矩阵运算.数值计算能力强大,但速度让人难以忍 受.软件立刻移植到C++上又不太实际,故采用 ...
- java中的注解详解和自定义注解
一.java中的注解详解 1.什么是注解 用一个词就可以描述注解,那就是元数据,即一种描述数据的数据.所以,可以说注解就是源代码的元数据.比如,下面这段代码: @Override public Str ...
- 学习Struts2.3
</pre>1.struts2.3核心jar包(若首次接触,可临时略过不看)大家能够到http://struts.apache.org/download.cgi#struts2014下载s ...
- VS2010中使用命令行參数
在Linux下编程习惯了使用命令行參数,故使用VS2010时也尝试了一下. 新建项目,c++编敲代码例如以下: #include<iostream> #include<fstream ...
- rundll32.exe的相关使用语句
命令列: rundll32.exe shell32.dll,Control_RunDLL 功能: 显示控制面板 命令列: rundll32.exe shell32.dll,Control_RunDLL ...
- Gradle项目学习 & HttpAsyncClient学习 & CountDownLatch学习
装了Intellij,就是装了Gradle. 新建一个Gradle项目.然后下面这个页面要勾选上面两项: Use auto-import和Create directories for empty co ...
- VR/AR工作原理、目前存在的技术问题
http://blog.csdn.net/liulong1567/article/details/50686558 摘要: 这些挑战,每一个都还需要很多努力才能解决.目前它们很多都还不到量变(只是需要 ...
- 【转】python中文转换url编码
今天要处理百度贴吧的东西.想要做一个关键词的list,每次需要时,直接添加 到list里面就可以了.但是添加到list里面是中文的情况(比如‘丽江’),url的地址编码却是'%E4%B8%BD%E6% ...
- Oracle Database Link 的创建和使用小见
假设:需要从数据库db_a通过db_link连接到db_b查询数据库b的部分相关信息 前提条件: 数据库a账户需要有创建dblink的权限,如果没有可以使用dba账户赋权限 grant CREATE ...