尝试用c#将JSON字符串转换为对象。使用一个非常简单的测试用例:

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
object routes_list = json_serializer.DeserializeObject("{ \"test\":\"some data\" }");

问题是routes_list从未被设置;它是一个未定义的对象。什么好主意吗?


当前回答

让我们假设你有一个名为Student的类,它有以下字段,它有一个将JSON作为输入并返回字符串Student Object的方法。我们可以使用JavaScriptSerializer在这里转换JSON字符串到c#对象。std是一个JSON字符串。

  public class Student
{
   public string FirstName {get;set:}
   public string LastName {get;set:}
   public int[] Grades {get;set:}
}

public static Student ConvertToStudent(string std)
{
  var serializer = new JavaScriptSerializer();

  Return serializer.Deserialize<Student>(std);
 }

其他回答

在c#中将JSON字符串转换为对象。使用下面的测试用例..这对我很管用。这里“MenuInfo”是我的c#类对象。

JsonTextReader reader = null;
try
{
    WebClient webClient = new WebClient();
    JObject result = JObject.Parse(webClient.DownloadString("YOUR URL"));
    reader = new JsonTextReader(new System.IO.StringReader(result.ToString()));
    reader.SupportMultipleContent = true;
}
catch(Exception)
{}

JsonSerializer serializer = new JsonSerializer();
MenuInfo menuInfo = serializer.Deserialize<MenuInfo>(reader);

看起来您正在尝试反序列化到原始对象。您可以创建一个Class来表示要转换的对象。这在处理较大对象或JSON字符串的情况下是最有用的。

例如:

  class Test {

      String test; 

      String getTest() { return test; }
      void setTest(String test) { this.test = test; }

  }

那么你的反序列化代码将是:

   JavaScriptSerializer json_serializer = new JavaScriptSerializer();
   Test routes_list = 
          (Test)json_serializer.DeserializeObject("{ \"test\":\"some data\" }");

更多信息可以在本教程中找到: http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-Csharp.aspx

正如tripletdad99所说

var result = JsonConvert.DeserializeObject<T>(json);

但如果你不想创建一个额外的对象,你可以用Dictionary来代替

var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(json_serializer);

添加这个ddl来引用您的项目:System.Web.Extensions.dll

使用System.Web.Script.Serialization;

public class IdName
{
    public int Id { get; set; }
    public string Name { get; set; }
}


   string jsonStringSingle = "{'Id': 1, 'Name':'Thulasi Ram.S'}".Replace("'", "\"");
   var entity = new JavaScriptSerializer().Deserialize<IdName>(jsonStringSingle);

   string jsonStringCollection = "[{'Id': 2, 'Name':'Thulasi Ram.S'},{'Id': 2, 'Name':'Raja Ram.S'},{'Id': 3, 'Name':'Ram.S'}]".Replace("'", "\"");
   var collection = new JavaScriptSerializer().Deserialize<IEnumerable<IdName>>(jsonStringCollection);

另一种快速简单的半自动这些步骤的方法是:

take the JSON you want to parse and paste it here: https://app.quicktype.io/ . Change language to C# in the drop down. Update the name in the top left to your class name, it defaults to "Welcome". In visual studio go to Website -> Manage Packages and use NuGet to add Json.Net from Newtonsoft. app.quicktype.io generated serialize methods based on Newtonsoft. Alternatively, you can now use code like: WebClient client = new WebClient(); string myJSON = client.DownloadString("https://URL_FOR_JSON.com/JSON_STUFF"); var myClass = Newtonsoft.Json.JsonConvert.DeserializeObject(myJSON);