-1

我有一个像这样的json字符串:

{"0":{"City_Id":"2","City_Name":"Agra"},"1":{"City_Id":"3","City_Name":"Ahmedabad"}}

我想以任何可能的方式在 c# 中反序列化它。(注意:对于 silverlight 4.0)

任何形式的帮助将不胜感激,

提前致谢!

4

1 回答 1

0

您可以使用 DataContractJsonSerializer 反序列化 json 字符串。

MemoryStream theObjectToBeDeserialized = new MemoryStream(Encoding.Unicode.GetBytes(jsonString))
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MyClass));
MyClass temp = (MyClass)serializer.ReadObject(theObjectToBeDeserialized);
theObjectToBeDeserialized.Dispose();

顺便一提; 最好用using;包裹语句

MyClass temp = null;
using(MemoryStream theObjectToBeDeserialized = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MyClass));
    temp = (MyClass)serializer.ReadObject(theObjectToBeDeserialized);
}

和相关的类:

public class City
{
    public int City_Id {get; set;}
    public string City_Name {get; set;}
}

和使用;

List<City> cities = null;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<City>));
cities = (List<City>)serializer.ReadObject(theObjectToBeDeserialized);
于 2013-07-22T11:10:50.730 回答