要使用您的代码,您应该使用类型化的转换
string jsondata = @"{""meta"":{""code"":200}}";
Meta json = JsonConvert.DeserializeObject<Meta>(jsondata);
然后您可以访问元 obj 中的所有成员。
使用普通的 .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
你需要有这个代码
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;
}
}