How to serialize a raw json field?

前端 未结 2 1539
不知归路
不知归路 2021-01-11 16:28

I have a field in the db that store a json string and I want that when I return it in a json result that will be returned as json raw data and not warped with quotes as str

2条回答
  •  执笔经年
    2021-01-11 16:37

    You will need to unserialize the data. C# offers a class to handle JSON data.

    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx

    Excerpt from http://msdn.microsoft.com/en-us/library/bb412179.aspx :

    Normally, JSON serialization and deserialization is handled automatically by Windows Communication Foundation (WCF) when you use data contract types in service operations that are exposed over AJAX-enabled endpoints. However, in some cases you may need to work with JSON data directly - this is the scenario that this topic demonstrates.

    //Deserialize the JSON-encoded data into a new instance of Person by using the ReadObject method of the DataContractJsonSerializer.
    
    stream1.Position = 0;   
    Person p2 = (Person)ser.ReadObject(stream1);
    
    //Show the results.
    
    Console.Write("Deserialized back, got name=");
    Console.Write(p2.name);
    Console.Write(", age=");
    Console.WriteLine(p2.age);
    

提交回复
热议问题