/**
* 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字符串和对象互转的更多相关文章

  1. Java——Json字符串与Object互转

    public static void JacksonTest() {//推荐 //{"MNG001":[{"ID":"1","PW ...

  2. jQuery 中json字符串与对象互转

    json字符串转json对象:jQuery.parseJSON(jsonStr); json对象转json字符串:JSON.stringify(jsonObj);

  3. java json字符串与对象转换

    下载引入包gson-2.0.jar 1.字符转数据 final Map map = new HashMap();map.put("contents",    "[{\&q ...

  4. Swift开发中 JSON对象/JSON字符串/Data的互转

    本文将介绍Swift开发中常用的转换(JSON对象/JSON字符串/Data之间的互相转换) #pragma mark - JSON(对象)----->JSON字符串 1.原生方法 //JSON ...

  5. Google Gson实现JSON字符串和对象之间相互转换

    User实体类 package com.test.json; /** * User 实体类 */ public class User { private String name; private St ...

  6. 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换

    [源码下载] 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 作者:webabcd 介绍速战速决 之 PHP ...

  7. json字符串和对象的相互转化

    json在代码中是经常用到的,在此总结一下json字符串和对象及数组之间的相互转化: 1.javascript函数方式: <1> JSON.stringify :把一个对象转换成json字 ...

  8. js在前端json字符串和对象互相转化

    js在前端json字符串和对象互相转化 //对象转json串 注意:参数必须是对象,数组不起作用,对象格式{'0'=>'a'} JSON.stringify( arr ); //json字符串转 ...

  9. 小程序json字符串转为对象

    小程序里json字符串转为对象使用JSON.parse()方法转变无效, 看报错提示有单引号“ ' ” 因为单引号而无效, 将单引号全改双引号即可. 报错如下: VM11050:1 thirdScri ...

随机推荐

  1. 【莫队算法】【权值分块】bzoj3809 Gty的二逼妹子序列

    如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; int ...

  2. 【莫队算法】bzoj3289 Mato的文件管理

    莫队算法,离线回答询问,按一定大小(sqrt(n*log(n))左右)将答案分块,按 ①左端点所在块②右端点 双关键字排序. 然后暴力转移. 转移的时候用树状数组. O(n*sqrt(n)*log(n ...

  3. 【Treap模板详细注释】BZOJ3224-普通平衡树

    模板题:D错因见注释 #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  4. 正版greenvpn

    短网址 http://jsq.re(建议收藏,长期有效)长网址 https://www.greenjsq.me/网址更新页面 http://www.greenvpn.site

  5. Tomcat部署时war和war exploded的区别

    转自徐刘根的Tomcat部署时war和war exploded区别以及平时踩得坑 一.war和war exploded的区别 在使用IDEA开发项目的时候,部署Tomcat的时候通常会出现下边的情况: ...

  6. [Java基础] Java如何实现条件编译

    条件编译绝对是一个好东西.如在C或CPP中,可以通过预处理语句来实现条件编译.但是在JAVA中却没有预处理,宏定义这些东西,而有时在一些项目中,我们又需要条件编译.那么,在JAVA中,该如何实现条件编 ...

  7. Kubernetes连接外部数据源

    Kubernetes架构下比较核心的问题是数据如何persistance,虽然提供了Persistent volumn的方式,但是对于像数据库之类的产品在kubernetes集群环境中运行和管理还是很 ...

  8. Coherence装载数据的研究-PreloadRequest

    最近给客户准备培训,看到Coherence可以通过三种方式批量加载数据,分别是: Custom application InvocableMap - PreloadRequest Invocation ...

  9. MySQL对时间的处理总结

    1.to_days函数查询今天的数据:select * from 表名 where to_days(时间字段名) = to_days(now()); to_days函数:返回从0000年(公元1年)至 ...

  10. 字典对象的 Pythonic 用法(上篇:转载)

    转载:https://mp.weixin.qq.com/s?timestamp=1498528588&src=3&ver=1&signature=DfFeOFPXy44ObCM ...