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

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

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


当前回答

看起来您正在尝试反序列化到原始对象。您可以创建一个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

其他回答

Newtonsoft比java脚本序列化器. ...更快这个依赖于Newtonsoft的NuGet包,这个包很流行,比默认的序列化器更好。

一行代码解决方案。

var myclass = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(Jsonstring);

Myclass oMyclass = Newtonsoft.Json.JsonConvert.DeserializeObject<Myclass>(Jsonstring);

在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);

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

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);

看起来您正在尝试反序列化到原始对象。您可以创建一个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

添加这个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);