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 ...
随机推荐
- 【KM算法】HDU2255-奔小康赚大钱
KM算法的裸体.O(n^4)的模板,实际上在增广路径的时候依然有冗余,可以用bfs优化到O(n^3). #include <iostream> #include <cstdio> ...
- Problem S: 零起点学算法14——三位数反转
#include<stdio.h> #include<stdlib.h> int main() { int a,b,c,s; scanf("%d",& ...
- 【MySQL笔记】: unable to connect to remote host. catalog download has failed.
安装完MySQL之后,它每天凌晨启动一个Intaller任务,甚是烦人: 这是一个Windows的计划服务,在这里删除即可,开始/附件/系统工具/任务计划程序,把mysql的定时任务计划取消/删除 ...
- JQuery中的动画(ppt)
<!DOCTYPE html> <html> <head> <title>test1.html</title> <meta http- ...
- iOS计算富文本(NSMutableAttributedString)高度
有时候开发中我们为了样式好看, 需要对文本设置富文本属性, 设置完后那么怎样计算其高度呢, 很简单, 方法如下: - (NSInteger)hideLabelLayoutHeight:(NSStrin ...
- Linux下#!/usr/bin/env bash和#!/usr/bin/bash、#!/bin/bash的比较
#!/usr/bin/env bash #在不同的系统上提供了一些灵活性. #!/usr/bin/bash #将对给定的可执行文件系统进行显式控制. 通过/usr/bin/env运行程序,用户不需要去 ...
- fedora25安装和docker-ce_清华源
docker-ce_清华源 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/fedora/ fedora25 docker-ce版本: htt ...
- scrapy-splash抓取动态数据例子十二
一.介绍 本例子用scrapy-splash通过搜狗搜索引擎,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二. ...
- 验收测试 - WebDriver 5
验收测试 - WebDriver - 配置 什么是WebDriver 这样说好了,它翻译起来就是Web驱动,用我的经验来说,它就是驱动浏览器运行的一个驱动器 有什么作用? 就像一个司机可以驱动一台汽车 ...
- [转载] 在Linux中,开机自动运行普通用户的脚本程序
FROM:http://blog.csdn.net/sinboy/article/details/2466225 FROM:http://www.2cto.com/os/201006/50680.ht ...