Convert JSON String To C# Object

2019-01-01 01:04发布

Trying to convert a JSON string into an object in C#. Using a really simple test case:

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

The problem is that routes_list never gets set; it's an undefined object. Any ideas?

标签: c# json
12条回答
浮光初槿花落
2楼-- · 2019-01-01 01:19

It looks like you're trying to deserialize to a raw object. You could create a Class that represents the object that you're converting to. This would be most useful in cases where you're dealing with larger objects or JSON Strings.

For instance:

  class Test {

      String test; 

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

  }

Then your deserialization code would be:

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

More information can be found in this tutorial: http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-Csharp.aspx

查看更多
闭嘴吧你
3楼-- · 2019-01-01 01:19

Or, you can use the Newtownsoft.Json library as follows:

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

Where T is your object type that matches your JSON string.

查看更多
余生无你
4楼-- · 2019-01-01 01:21

Another fast and easy way to semi-automate these steps is to:

  1. take the JSON you want to parse and paste it here: http://json2csharp.com/ then copy and paste the resulting into a new class (ex: MyClass) in visual studio.
  2. Rename "RootObject" in the output from json2csharp to "MyClass" or whatever you called it.
  3. In visual studio go to Website -> Manage Packages and use NuGet to add Json.Net from Newtonsoft.

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);
查看更多
美炸的是我
5楼-- · 2019-01-01 01:27

add this ddl to reference to your project: System.Web.Extensions.dll

use this namespace: using 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);
查看更多
姐姐魅力值爆表
6楼-- · 2019-01-01 01:28

As tripletdad99 said

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

but if you don't want to create an extra object you can make it with Dictionary instead

var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(json_serializer);
查看更多
旧人旧事旧时光
7楼-- · 2019-01-01 01:30

Newtonsoft is faster than java script serializer. ... this one depends on the Newtonsoft NuGet package, which is popular and better than the default serializer.

one line code solution.

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

Myclass oMyclass = Newtonsoft.Json.JsonConvert.DeserializeObject<Myclass>(Jsonstring);
查看更多
登录 后发表回答