Convert JSON String To C# Object

前端 未结 14 2145
感情败类
感情败类 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:49

    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

提交回复
热议问题