反序列化json数组以列出 [英] Deserialize json array to list
本文介绍了反序列化json数组以列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的Json对象如下所示,
Hi All,
My Json object looks like below,
[{"key":"field1","value":false},{"key":"field2","value":false},{"key":"field3","value":false},{"key":"field4","value":false},{"key":"field7","value":false},{"key":"field8","value":false}]
我反序列化了
I am deserialize it like
JavaScriptSerializer serialize = new JavaScriptSerializer();
List<KeyValuePair<string, string>> obj = serialize.Deserialize<List<KeyValuePair<string, string>>>(Element);
但是,要获得空名单,我不知道为什么,我有什么遗漏吗?
谢谢,
Rachana
But getting empty list, I don''t know why , Am I missinbg anything?
Thanks,
Rachana
推荐答案
我没有发现代码有很多错误,因此我认为您的问题可能与序列化本身有关.我有几个用PHP编写的网站,这些网站使用JSON序列化和加密的数据来提取通过这些网站收到的订单,而不是单独进入每个数据库来处理订单.我是通过C#应用程序中的数据合同来实现的.
我附上了几节课的摘录,例如,希望它们能帮助回答您的问题.
首先,我设置了我的数据合同类
I don''t see a whole lot wrong with the code so I''m thinking your problem might be with the serialization itself. I have several websites written in PHP that I use JSON serialization and encrypted data to pull down orders that have come in through the sites instead of going to each database individually to process the orders. I accomplish this through Data Contracts in a C# app.
I''ve attached some snippets from a couple of my classes for example I hope they help answer your question.
First I set up a my data contract class
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace WebOrders
{
[DataContract]
public class OrderItem
{
public OrderItem() { }
[DataMember]
public string orderID { get; set; }
[DataMember]
public string qty { get; set; }
[DataMember]
public string productID { get; set; }
[DataMember]
public string mfrPartNumber { get; set; }
[DataMember]
public string price { get; set; }
}
}
然后,我使用一个具有通用枚举器的实用程序类来处理所有序列化和反序列化事务...
Then I use a utility class that has a generic enumerator to handle all serialization and deserialization transactions...
public static IEnumerable<t> GetValues<t>()
{
return Enum.GetValues(typeof(T)).Cast<t>();
}
public static string Serialize<t>(T obj)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, obj);
string ret = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
return ret;
}
public static T Deserialize<t>(string json)
{
T obj = Activator.CreateInstance<t>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
ms.Dispose();
return obj;
}
用法如下:
Usage is as follows:
OrderItem orderitem = Deserialize<OrderItem>(o);
希望您觉得这有用,我知道我确实做到了...
-史密斯
Hope you find this useful, I know I sure did...
-Smith
这篇关于反序列化json数组以列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文