string jsondata = @"{""meta"":{""code"":200}}";
dynamic json = JsonConvert.DeserializeObject(jsondata);
我有上面的json数据,我为它创建了类。我还对它进行了反序列化——我如何迭代这个json变量
public class Meta
{
public int code { get; set; }
}
public class RootObject
{
public Meta meta { get; set; }
}
要使用您的代码,您应该使用键入的convert
string jsondata = @"{""meta"":{""code"":200}}";
Meta json = JsonConvert.DeserializeObject<Meta>(jsondata);
然后你可以访问元对象中的所有成员。
使用普通的.net json运行时,您可以反序列化字符串
string jsondata = @"{""meta"":{""code"":200}}"
Meta meta = JsonHelper.JsonDeserialize<Meta>(jsondata);
有关更多信息,请参阅http://www.codeproject.com/Articles/272335/JSON-Serialization-and-Deserialization-in-ASP-NET
你需要有这个代码的C
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
/// <summary>
/// JSON Serialization and Deserialization Assistant Class
/// </summary>
public class JsonHelper
{
/// <summary>
/// JSON Serialization
/// </summary>
public static string JsonSerializer<T> (T t)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, t);
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return jsonString;
}
/// <summary>
/// JSON Deserialization
/// </summary>
public static T JsonDeserialize<T> (string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
}