Convert JSON String To C# Object

前端 未结 14 2143
感情败类
感情败类 2020-11-22 14:27

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

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
obj         


        
14条回答
  •  孤城傲影
    2020-11-22 14:43

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

    1. take the JSON you want to parse and paste it here: https://app.quicktype.io/ . Change language to C# in the drop down.
    2. Update the name in the top left to your class name, it defaults to "Welcome".
    3. In visual studio go to Website -> Manage Packages and use NuGet to add Json.Net from Newtonsoft.
    4. 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);

提交回复
热议问题