Convert JSON String To C# Object

前端 未结 14 1935
感情败类
感情败类 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 15:00

    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);
    
    0 讨论(0)
  • 2020-11-22 15:01

    Using dynamic object with JavaScriptSerializer.

    JavaScriptSerializer serializer = new JavaScriptSerializer(); 
    dynamic item = serializer.Deserialize<object>("{ \"test\":\"some data\" }");
    string test= item["test"];
    
    //test Result = "some data"
    
    0 讨论(0)
提交回复
热议问题