json序列化和c json反序列化化的不同

在C#中,Json的序列化和反序列化的几种方式总结
在这篇文章中,我们将会学到如何使用C#,来序列化对象成为Json格式的数据,以及如何反序列化Json数据到对象。
什么是JSON?
JSON (Java Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and easy for machines to parse and generate. JSON is a text format that is completely language independent.
翻译:Json【java对象表示方法】,它是一个轻量级的数据交换格式,我们可以很简单的来读取和写它,并且它很容易被计算机转化和生成,它是完全独立于语言的。
Json支持下面两种数据结构:
键值对的集合--各种不同的编程语言,都支持这种数据结构;
有序的列表类型值的集合--这其中包含数组,集合,矢量,或者序列,等等。
Json有下面几种表现形式
一个没有顺序的“键/值”,一个对象以花括号“{”开始,并以花括号"}"结束,在每一个“键”的后面,有一个冒号,并且使用逗号来分隔多个键值对。例如:
var user = {"name":"Manas","gender":"Male","birthday":""}
设置值的顺序,一个数组以中括号"["开始,并以中括号"]"结束,并且所有的值使用逗号分隔,例如:
var userlist = [{"user":{"name":"Manas","gender":"Male","birthday":""}},
{"user":{"name":"Mohapatra","Male":"Female","birthday":""}}]
任意数量的Unicode字符,使用引号做标记,并使用反斜杠来分隔。例如:
var userlist = "{"ID":1,"Name":"Manas","Address":"India"}"
好了,介绍完JSON,现在说正题,我们事先序列化和反序列化有三种方式:
1.使用JavaSerializer类
2.使用DataContractJsonSerializer类
3.使用JSON.NET类库
我们先来看看使用 DataContractJsonSerializer的情况
DataContractJsonSerializer类帮助我们序列化和反序列化Json,他在程序集 System.Runtime.Serialization.dll下的System.Runtime.Serialization.Json命名空间里。
首先,这里,我新建一个控制台的程序,新建一个类Student
using System.Collections.G
using System.L
using System.T
using System.Threading.T
using System.Runtime.S
namespace JsonSerializerAndDeSerializer
[DataContract]
public class Student
[DataMember]
public int ID { }
[DataMember]
public string Name { }
[DataMember]
public int Age { }
[DataMember]
public string Sex { }
注意:上面的Student实体中的契约 [DataMember],[DataContract],是使用DataContractJsonSerializer序列化和反序列化必须要加的,对于其他两种方式不必加,也可以的。
我们程序的代码:
要先引用程序集,在引入这个命名空间
//----------------------------------------------------------------------------------------------//使用DataContractJsonSerializer方式需要引入的命名空间,在System.Runtime.Serialization.dll.中using System.Runtime.Serialization.J//--------------------------------------------------------------------------------------------
#region 1.DataContractJsonSerializer方式序列化和反序列化
Student stu = new Student()
Name = "曹操",
Sex = "男",
Age = 1000
}; //序列化
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Student));
MemoryStream msObj = new MemoryStream(); //将序列化之后的Json格式数据写入流中 js.WriteObject(msObj, stu);
msObj.Position = 0;
//从0这个位置开始读取流中的数据
StreamReader sr = new StreamReader(msObj, Encoding.UTF8); string json = sr.ReadToEnd();
sr.Close();
msObj.Close();
Console.WriteLine(json);
//反序列化
string toDes =
//string to = "{"ID":"1","Name":"曹操","Sex":"男","Age":"1230"}";
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(toDes)))
DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(Student));
Student model = (Student)deseralizer.ReadObject(ms);// //反序列化ReadObject
Console.WriteLine("ID=" + model.ID);
Console.WriteLine("Name=" + model.Name);
Console.WriteLine("Age=" + model.Age);
Console.WriteLine("Sex=" + model.Sex);
Console.ReadKey();
#endregion
运行之后结果是:
再看看使用JavaJsonSerializer的情况:
JavaSerializer is a class which helps toserialize and deserialize JSON. It is present in namespace System.Web..Serializationwhich is available in assembly System.Web.Extensions.dll. To serialize a .Net object to JSON string use Serialize method. It's possible to deserialize JSON string to .Net object using Deserialize&T&or DeserializeObject methods. Let's see how to implement serialization and deserialization using JavaSerializer.
这里要先引用
//-----------------------------------------------------------------------------------------//使用JavaSerializer方式需要引入的命名空间,这个在程序集System.Web.Extensions.dll.中using System.Web..S//----------------------------------------------------------------------------------------
#region 2.JavaSerializer方式实现序列化和反序列化
Student stu = new Student()
Name = "关羽",
Age = 2000,
Sex = "男"
JavaSerializer js = new JavaSerializer();
string jsonData = js.Serialize(stu);//序列化 Console.WriteLine(jsonData);
////反序列化方式一:
string desJson = jsonD
//Student model = js.Deserialize&Student&(desJson);// //反序列化
//string message = string.Format("ID={0},Name={1},Age={2},Sex={3}", model.ID, model.Name, model.Age, model.Sex);
//Console.WriteLine(message); //Console.ReadKey();
////反序列化方式2
dynamic modelDy = js.Deserialize&dynamic&(desJson); //反序列化
string messageDy = string.Format("动态的反序列化,ID={0},Name={1},Age={2},Sex={3}",
modelDy["ID"], modelDy["Name"], modelDy["Age"], modelDy["Sex"]);//这里要使用索引取值,不能使用对象.属性
Console.WriteLine(messageDy);
Console.ReadKey();
#endregion
最后看看使用JSON.NET的情况,引入类库:
下面的英文,看不懂可略过。。。
Json.NET is a third party library which helps conversion between JSON text and .NET object using the JsonSerializer. The JsonSerializer converts .NET objects into their JSON equivalent text and back again by mapping the .NET object property names to the JSON property names. It is open source software and free for commercial purposes.
The following are some awesome【极好的】 features,
Flexible JSON serializer for converting between .NET objects and JSON.
LINQ to JSON for manually reading and writing JSON.
High performance, faster than .NET's built-in【内嵌】 JSON serializers.
Easy to read JSON.
Convert JSON to and from XML.
Supports .NET 2, .NET 3.5, .NET 4, Silverlight and Windows Phone.
Let’s start learning how to install and implement:
In Visual Studio, go to Tools Menu -& Choose Library Package Manger -& Package Manager Console. It opens a command window where we need to put the following command to install Newtonsoft.Json.
Install-Package Newtonsoft.Json
In Visual Studio, Tools menu -& Manage Nuget Package Manger Solution and type “JSON.NET” to search it online. Here's the figure,
//使用Json.NET类库需要引入的命名空间//-----------------------------------------------------------------------------using Newtonsoft.J//-------------------------------------------------------------------------
#region 3.Json.NET序列化
List&Student& lstStuModel = new List&Student&()
new Student(){ID=1,Name="张飞",Age=250,Sex="男"}, new Student(){ID=2,Name="潘金莲",Age=300,Sex="女"}
//Json.NET序列化
string jsonData = JsonConvert.SerializeObject(lstStuModel);
Console.WriteLine(jsonData);
Console.ReadKey();
//Json.NET反序列化
string json = @"{ 'Name':'C#','Age':'3000','ID':'1','Sex':'女'}";
Student descJsonStu = JsonConvert.DeserializeObject&Student&(json);
//反序列化
Console.WriteLine(string.Format("反序列化: ID={0},Name={1},Sex={2},Sex={3}", descJsonStu.ID, descJsonStu.Name, descJsonStu.Age, descJsonStu.Sex));
Console.ReadKey();
#endregion
运行之后,结果是:
总结:最后还是尽量使用JSON.NET来序列化和反序列化,性能好。
.NET开发,前端设计,微信中搜索趣味CSharp或扫描二维码关注
责任编辑:
声明:本文由入驻搜狐号的作者撰写,除搜狐官方账号外,观点仅代表作者本人,不代表搜狐立场。
今日搜狐热点ASP.NET中JSON的序列化和反序列化
发表于 14:30|
来源博客园|
作者Asharp
摘要:导读:JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式。在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍,ASP
导读:JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式。在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍,ASP.NET如何序列化和反序列化的处理,在序列化和反序列化对日期时间、集合、字典的处理。
一、JSON简介
JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式。
JSON是&名值对&的集合。结构由大括号'{}',中括号'[]',逗号',',冒号':',双引号'&&'组成,包含的数据类型有Object,Number,Boolean,String,Array, NULL等。
JSON具有以下的形式:
对象(Object)是一个无序的&名值对&集合,一个对象以&{&开始,&}&结束。每个&名&后跟着一个&:&,多个&名值对&由逗号分隔。如:
var user={&name&:&张三&,&gender&:&男&,&birthday&:&&}
数组(Array)是值的有序集合,一个数组以&[&开始,以&]&结束,值之间使用&,&分隔。如:
var userlist=[{&user&:{&name&:&张三&,&gender&:&男&,&birthday&:&&}},{&user&:{&name&:&李四&,&gender&:&男&,&birthday&:&&}}];
字符串(String)是由双引号包围的任意数量的Unicode字符的集合,使用反斜线转义。
二、对JSON数据进行序列化和反序列化
可以使用DataContractJsonSerializer类将类型实例序列化为JSON字符串,并将JSON字符串反序列化为类型实例。 DataContractJsonSerializer在System.Runtime.Serialization.Json命名空间下,.NET Framework 3.5包含在System.ServiceModel.Web.dll中,需要添加对其的引用;.NET Framework 4在System.Runtime.Serialization中。
利用DataContractJsonSerializer序列化和反序列化的代码:
&& 1: using S
&& 2: using System.Collections.G
&& 3: using System.L
&& 4: using System.W
&& 5: using System.Runtime.Serialization.J
&& 6: using System.IO;
&& 7: using System.T
&& 9: /// &summary&
& 10: /// JSON序列化和反序列化辅助类
& 11: /// &/summary&
& 12: public class JsonHelper
& 14:&&&& /// &summary&
& 15:&&&& /// JSON序列化
& 16:&&&& /// &/summary&
& 17:&&&& public static string JsonSerializer&T&(T t)
& 18:&&&& {
& 19:&&&&&&&& DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
& 20:&&&&&&&& MemoryStream ms = new MemoryStream();
& 21:&&&&&&&& ser.WriteObject(ms, t);
& 22:&&&&&&&& string jsonString = Encoding.UTF8.GetString(ms.ToArray());
& 23:&&&&&&&& ms.Close();
& 24:&&&&&&&& return jsonS
& 25:&&&& }
& 27:&&&& /// &summary&
& 28:&&&& /// JSON反序列化
& 29:&&&& /// &/summary&
& 30:&&&& public static T JsonDeserialize&T&(string jsonString)
& 31:&&&& {
& 32:&&&&&&&& DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
& 33:&&&&&&&& MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
& 34:&&&&&&&& T obj = (T)ser.ReadObject(ms);
& 35:&&&&&&&&
& 36:&&&& }
序列化Demo:
简单对象Person:
&& 1: public class Person
&& 3:&&&& public string Name { }
&& 4:&&&& public int Age { }
序列化为JSON字符串:
&& 1: protected void Page_Load(object sender, EventArgs e)
&& 3:&&&& Person p = new Person();
&& 4:&&&& p.Name = &张三&;
&& 5:&&&& p.Age = 28;
&& 7:&&&& string jsonString = JsonHelper.JsonSerializer&Person&(p);
&& 8:&&&& Response.Write(jsonString);
输出结果:
{&Age&:28,&Name&:&张三&}
反序列化Demo:
&& 1: protected void Page_Load(object sender, EventArgs e)
&& 3:&&&& string jsonString = &{\&Age\&:28,\&Name\&:\&张三\&}&;
&& 4:&&&& Person p = JsonHelper.JsonDeserialize&Person&(jsonString);
运行结果:
ASP.NET中的JSON序列化和反序列化还可以使用JavaScriptSerializer,在 System.Web.Script.Serializatioin命名空间下,需引用System.Web.Extensions.dll.也可以使用 JSON.NET.
三、JSON序列化和反序列化日期时间的处理
JSON格式不直接支持日期和时间。DateTime值值显示为&/Date(0)/&形式的JSON字符串,其中第一个数字(在提 供的示例中为 700000)是 GMT 时区中自 1970 年 1 月 1 日午夜以来按正常时间(非夏令时)经过的毫秒数。该数字可以是负数,以表示之前的时间。示例中包括&+0500&的部分可选,它指示该时间属于Local 类型,即它在反序列化时应转换为本地时区。如果没有该部分,则会将时间反序列化为Utc。
修改Person类,添加LastLoginTime:
&& 1: public class Person
&& 3:&&&& public string Name { }
&& 4:&&&& public int Age { }
&& 5:&&&& public DateTime LastLoginTime { }
&& 1: Person p = new Person();
&& 2: p.Name = &张三&;
&& 3: p.Age = 28;
&& 4: p.LastLoginTime = DateTime.N
&& 6: string jsonString = JsonHelper.JsonSerializer&Person&(p);
序列化结果:
{&Age&:28,&LastLoginTime&:&\/Date(8+0800)\/&,&Name&:&张三&}
1. 在后台使用正则表达式对其替换处理。修改JsonHelper:
&& 1: using S
&& 2: using System.Collections.G
&& 3: using System.L
&& 4: using System.W
&& 5: using System.Runtime.Serialization.J
&& 6: using System.IO;
&& 7: using System.T
&& 8: using System.Text.RegularE
& 10: /// &summary&
& 11: /// JSON序列化和反序列化辅助类
& 12: /// &/summary&
& 13: public class JsonHelper
& 15:&&&& /// &summary&
& 16:&&&& /// JSON序列化
& 17:&&&& /// &/summary&
& 18:&&&& public static string JsonSerializer&T&(T t)
& 19:&&&& {
& 20:&&&&&&&& DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
& 21:&&&&&&&& MemoryStream ms = new MemoryStream();
& 22:&&&&&&&& ser.WriteObject(ms, t);
& 23:&&&&&&&& string jsonString = Encoding.UTF8.GetString(ms.ToArray());
& 24:&&&&&&&& ms.Close();
& 25:&&&&&&&& //替换Json的Date字符串
& 26:&&&&&&&& string p = @&\\/Date\((\d+)\+\d+\)\\/&;
& 27:&&&&&&&& MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
& 28:&&&&&&&&& Regex reg = new Regex(p);
& 29:&&&&&&&& jsonString = reg.Replace(jsonString, matchEvaluator);
& 30:&&&&&&&& return jsonS
& 31:&&&& }
& 33:&&&& /// &summary&
& 34:&&&& /// JSON反序列化
& 35:&&&& /// &/summary&
& 36:&&&& public static T JsonDeserialize&T&(string jsonString)
& 37:&&&& {
& 38:&&&&&&&& //将&yyyy-MM-dd HH:mm:ss&格式的字符串转为&\/Date(8+0800)\/&格式
& 39:&&&&&&&& string p = @&\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}&;
& 40:&&&&&&&& MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
& 41:&&&&&&&& Regex reg = new Regex(p);
& 42:&&&&&&&& jsonString = reg.Replace(jsonString, matchEvaluator);
& 43:&&&&&&&& DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
& 44:&&&&&&&& MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
& 45:&&&&&&&& T obj = (T)ser.ReadObject(ms);
& 46:&&&&&&&&
& 47:&&&& }
& 49:&&&& /// &summary&
& 50:&&&& /// 将Json序列化的时间由/Date(8+0800)转为字符串
& 51:&&&& /// &/summary&
& 52:&&&& private static string ConvertJsonDateToDateString(Match m)
& 53:&&&& {
& 54:&&&&&&&& string result = string.E
& 55:&&&&&&&& DateTime dt = new DateTime();
& 56:&&&&&&&& dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
& 57:&&&&&&&& dt = dt.ToLocalTime();
& 58:&&&&&&&& result = dt.ToString(&yyyy-MM-dd HH:mm:ss&);
& 59:&&&&&&&&
& 60:&&&& }
& 62:&&&& /// &summary&
& 63:&&&& /// 将时间字符串转为Json时间
& 64:&&&& /// &/summary&
& 65:&&&& private static string ConvertDateStringToJsonDate(Match m)
& 66:&&&& {
& 67:&&&&&&&& string result = string.E
& 68:&&&&&&&& DateTime dt = DateTime.Parse(m.Groups[0].Value);
& 69:&&&&&&&& dt = dt.ToUniversalTime();
& 70:&&&&&&&& TimeSpan ts = dt - DateTime.Parse(&&);
& 71:&&&&&&&& result = string.Format(&\\/Date({0}+0800)\\/&,ts.TotalMilliseconds);
& 72:&&&&&&&&
& 73:&&&& }
序列化Demo:
&& 1: Person p = new Person();
&& 2: p.Name = &张三&;
&& 3: p.Age = 28;
&& 4: p.LastLoginTime = DateTime.N
&& 6: string jsonString = JsonHelper.JsonSerializer&Person&(p);
运行结果:
{&Age&:28,&LastLoginTime&:& 01:00:56&,&Name&:&张三&}
反序列化Demo:
string json = &{\&Age\&:28,\&LastLoginTime\&:\& 00:30:00\&,\&Name\&:\&张三\&}&;
p=JsonHelper.JsonDeserialize&Person&(json);
运行结果:
在后台替换字符串适用范围比较窄,如果考虑到全球化的有多种语言还会更麻烦。
2. 利用JavaScript处理
&& 1: function ChangeDateFormat(jsondate) {
&& 2:&&&& jsondate = jsondate.replace(&/Date(&, &&).replace(&)/&, &&);
&& 3:&&&& if (jsondate.indexOf(&+&) & 0) {
&& 4:&&&&&&&& jsondate = jsondate.substring(0, jsondate.indexOf(&+&));
&& 5:&&&& }
&& 6:&&&& else if (jsondate.indexOf(&-&) & 0) {
&& 7:&&&&&&&& jsondate = jsondate.substring(0, jsondate.indexOf(&-&));
&& 8:&&&& }
& 10:&&&& var date = new Date(parseInt(jsondate, 10));
& 11:&&&& var month = date.getMonth() + 1 & 10 ? &0& + (date.getMonth() + 1) : date.getMonth() + 1;
& 12:&&&& var currentDate = date.getDate() & 10 ? &0& + date.getDate() : date.getDate();
& 13:&&&& return date.getFullYear() + &-& + month + &-& + currentD
简单Demo :
ChangeDateFormat(&\/Date(8+0800)\/&);
四、JSON序列化和反序列化集合、字典、数组的处理
在JSON数据中,所有的集合、字典和数组都表示为数组。
List&T&序列化:
&& 1: List&Person& list = new List&Person&()
&& 3:&&&& new Person(){ Name=&张三&, Age=28},
&& 4:&&&& new Person(){ Name=&李四&, Age=25}
&& 7: string jsonString = JsonHelper.JsonSerializer&List&Person&&(list);
序列化结果:
&[{\&Age\&:28,\&Name\&:\&张三\&},{\&Age\&:25,\&Name\&:\&李四\&}]&
字典不能直接用于JSON,Dictionary字典转化为JSON并不是跟原来的字典格式一致,而是形式以Dictionary的Key作为名称&Key&的值,以Dictionary的Value作为名称为&Value&的值 。如:
&& 1: Dictionary&string, string& dic = new Dictionary&string, string&();
&& 2: dic.Add(&Name&, &张三&);
&& 3: dic.Add(&Age&, &28&);
&& 5: string jsonString = JsonHelper.JsonSerializer & Dictionary&string, string&&(dic);
序列化结果:
&& 1: &[{\&Key\&:\&Name\&,\&Value\&:\&张三\&},{\&Key\&:\&Age\&,\&Value\&:\&28\&}]&
原文链接:
推荐阅读相关主题:
网友评论有(0)
CSDN官方微信
扫描二维码,向CSDN吐槽
微信号:CSDNnews
相关热门文章json-lib 序列化和反序列化 - 简单的幸福 - ITeye博客
博客分类:
可以使用json-lib来序列化java对象
依赖的jar包:
如何使用json-lib来序列化java对象呢?
public void test_serialize(){
Class2 c=new Class2();
List&Student&students=new ArrayList&Student&();
Student student=new Student();
Map&String, Object& attribute =new HashMap&String, Object&();
attribute.put("p1", "v1");
attribute.put("p2", "v2");
student.setAttribute(attribute);
students.add(student);
c.setStudents(students);
c.setClassName("计算机0705");
JSONObject js = JSONObject.fromObject(c/*, jsonConfig*/);
System.out.println(js.toString());
运行结果如下:
{"classAttribute":null,"className":"计算机0705","count":0,"students":[{"addrr":null,"age":0,"attribute":{"p2":"v2","p1":"v1"},"hobby":"","name":""}]}
public void test_serialize2(){
Class2 c=new Class2();
List&Student&students=new ArrayList&Student&();
Student student=new Student();
Map&String, Object& attribute =new HashMap&String, Object&();
attribute.put("p1", "v1");
attribute.put("p2", "v2");
student.setAttribute(attribute);
students.add(student);
c.setStudents(students);
c.setClassName("计算机0705");
Map&String, Object& classAttribute =new HashMap&String, Object&();
classAttribute.put("pp1", "vv1");
classAttribute.put("pp2", "vv2");
Teacher t=new Teacher();
t.setName("熊应标");
t.setTitle("语文老师");
c.setClassAttribute(classAttribute);
One2One one=new One2One();
one.setC(c);
one.setT(t);
JSONObject js = JSONObject.fromObject(c/*, jsonConfig*/);
System.out.println(js.toString());
运行结果如下:
{"classAttribute":{"pp1":"vv1","pp2":"vv2"},"className":"计算机0705","count":0,"students":[{"addrr":null,"age":0,"attribute":{"p2":"v2","p1":"v1"},"hobby":"","name":""}]}
如何使用json-lib反序列化(把string转化为Java对象)?
参考:/admin/blogs/1993048
void test_reserialize(){
String jsonInput="{\"className\":\"计算机0705\",\"count\":0,\"students\":[{\"addrr\":null,\"age\":0,\"hobby\":\"\",\"name\":\"\"}]}";
String jsonInput="{\"classAttribute\":{\"pp1\":\"vv1\",\"pp2\":\"vv2\"},\"className\":\"计算机0705\",\"count\":0,\"students\":[{\"addrr\":null,\"age\":0,\"attribute\":{\"p2\":\"v2\",\"p1\":\"v1\"},\"hobby\":\"\",\"name\":\"\"}]}";
JSONObject js = JSONObject.fromObject(jsonInput);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setRootClass(Class2.class);
Map&String, Class& classMap = new HashMap&String, Class&();
classMap.put("students", Student.class); // 指定JsonRpcRequest的request字段的内部类型
jsonConfig.setClassMap(classMap);
Class2 one = (Class2) JSONObject.toBean(js, jsonConfig/*java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to*/);
System.out.println(one.getClassName());
Map&String, Object& attribute =one.getStudents().get(0).getAttribute();
System.out.println(attribute);
运行结果:
计算机0705
{p2=v2, p1=v1}
序列化时如何删除属性值为null的属性
下面的事不合要求的:
{"addrr":{"country":"中国","state":"湖北省","street":"清河"},"age":25,"hobby":"","name":"黄威"}
hobby的值为空,应该被过滤掉的。
解决方法:
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
publicboolean apply(Object source, String name, Object value) {
// System.out.println(name);
// System.out.println(value);
if (value == null || (value instanceof String)
&& ((String) value).equals("")) {
returntrue;
returnfalse;
JSONObject js = JSONObject.fromObject(student, jsonConfig);
参考:/blog/1997956
源代码见附件
下载次数: 25
浏览: 2527402 次
来自: 北京
新传的jar包还是空的
ipodao 写道解决了我的问题,谢谢你。能帮到你,我很高兴
解决了我的问题,谢谢你。
说得不清不楚,谁看得懂?
java中直接加Math.random()就好,&scr ...

我要回帖

更多关于 java json反序列化 的文章

 

随机推荐