如何在没有newtonsoft的情况下序列化和反序列化 [英] How do you serialize and deserialize without newtonsoft
本文介绍了如何在没有newtonsoft的情况下序列化和反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果没有Newtonsoft,您如何序列化和反序列化?这是我的尝试。
var serializer = 新 JavaScriptSerializer();
var json1 = [count:{第一:1,第二:2,第三:3},{第一:11,第二:22,第三:33},{第一:111,第二:222,第三:333}]跨度>;
var jsons = serializer.Serialize(json1);
dynamic jsona = serializer.Deserialize(json1, typeof (对象跨度>));
第二部分:如何提取数据:
for each(jsone in jsona)
n + = 1 ;
arr( 1 ,n)= jsone.first;
arr( 2 ,n)= jsone.second;
arr( 3 ,n)= jsone.last;
next
我的尝试:
尝试安装Newtonsoft。不成功。
这个简单的Javasciptserilizer也不起作用,但它是我发现的最好和最简单的。
解决方案
<击> [数:{第一:1,第二:2,第三:3},{第一:11,第二:22,第三:33},{第一:111,第二:222,第三:333} ]
以上不是有效的json格式,应该是
var json1 ={count:[{first: 1,第二:2,第三:3},{第一:11,第二:22,第三:33},{第一:111,第二:222,第三:333}]};
并反序列化为
公共类MyClass
{
公共号码[] count {get;组; }
}
公共类号
{
public int first {get;组; }
public int second {get;组; }
public int third {get;组; }
}
static void Main()
{
var serializer = new JavaScriptSerializer();
var json1 ={count:[{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third :333}]};
MyClass jsona =(MyClass)serializer.Deserialize(json1,typeof(MyClass));
foreach(jsona.count中的数字项目)
{
var first = item.first;
var second = item.second;
var third = item.third;
}
}
How do you serialize and deserialize without Newtonsoft? This is my attempt at it.
var serializer = new JavaScriptSerializer();
var json1 = "[count:{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third:333}]";
var jsons = serializer.Serialize(json1);
dynamic jsona = serializer.Deserialize(json1, typeof(object));
Second Part: how to extract the data:
for each (jsone in jsona)
n+=1;
arr(1,n)=jsone.first;
arr(2,n)=jsone.second;
arr(3,n)=jsone.last;
next
What I have tried:
Tried installing Newtonsoft. Unsucessful.
This simple Javasciptserilizer is not working either but it is the best and simplest i have found.
解决方案
[count:{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third:333}]
the above is not a valid json format, it should be
var json1 = "{count:[{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third:333}]}";
and deserialized as
public class MyClass { public Number[] count { get; set; } } public class Number { public int first { get; set; } public int second { get; set; } public int third { get; set; } } static void Main() { var serializer = new JavaScriptSerializer(); var json1 = "{count:[{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third:333}]}"; MyClass jsona = (MyClass)serializer.Deserialize(json1, typeof(MyClass)); foreach (Number item in jsona.count) { var first = item.first; var second = item.second; var third = item.third; } }
这篇关于如何在没有newtonsoft的情况下序列化和反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文