WCF - How to serialize and deserialize in JSON?

I have written several classes (data and service contracts) in WCF, and I am trying to serialize and deserialize in JSON. If I need the following JSON structure, how would I create a DataContract (s):

{
  "response": {
    "locations": {
      "location": [
        {
          "id": "12",
          "name": "Hello",
          "statusid": "78"
        },
        {
          "id": "5",
          "name": "Ann",
          "statusid": "8"
        }
      ]
    },
    "error": "404 error"
  }
}

The structure given above is quite simple, and there can be some location data by location, as indicated above. Therefore, I need to get a list of arrays / lists for "locations" as follows. At the moment, I only have the following DataContract:

[DataContract]
    public class Response
    {
        [DataMember]
        public string locations { get; set; }

        [DataMember]
        public string error{ get; set; }
    }

Please let me know how can I solve this?

+5
source share
3 answers

The complete objects you are looking for should be structured as:

[DataContract(Name="response")]
public class Response
{
    [DataMember(Name = "locations")]
    public IEnumerable<Location> Locations { get; set; }

    [DataMember(Name = "error")]
    public string Error { get; set; }
}

[DataContract(Name = "location")]
public class Location
{
    [DataMember(Name = "id")]
    public string Id { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "statusid")]
    public string StatusId { get; set; }
}

, { }, IEnumerable/array, [ ] JSON.

, , , , JSON . , , JSON XML Serialization - ASP.NET.

vittore , JSON , :

[DataContract]
public class ResponseParent
{
    [DataMember(Name = "response")]
    public Response ResponseInstance { get; set; }
}

[DataContract]
public class Response
{
    [DataMember(Name = "locations")]
    public LocationCollectionIntermediate Locations { get; set; }

    [DataMember(Name = "error")]
    public string Error { get; set; }
}

[DataContract]
public class LocationCollectionIntermediate
{
    [DataMember(Name = "location")]
    public IEnumerable<Location> Locations { get; set; }
}

[DataContract]
public class Location
{
    [DataMember(Name = "id")]
    public string Id { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "statusid")]
    public string StatusId { get; set; }
}

, . , JSON, Id int.

, WCF JSON, :

string json;
using (var ms = new MemoryStream())
{
    var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(ResponseParent));
    ser.WriteObject(ms, r);
    json = System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length)); 
}

DataContractJsonSerializer

, - RESTful, JSON WCF RESTful Service 60 .

+10

http://json.codeplex.com/.

JsonConvert.SerializeObject()

JSON.

JsonConvert.PopulateObject

JSON .

+3

Once you have the exact structure, you can also use the JavaScriptSerializer class from System.Web.Script.Serialization(starting with .NET 3.5)

Something like this will work:

string jsonString;

JavaScriptSerializer serializer = new JavaScriptSerializer();
Response responseObject = serializer.Deserialize<Response>(jsonString);

Even easier than using DataContractSerializer, although I'm not quite sure about its reservations. I used it several times without any problems.

You can also add custom JavaScriptConverter s, which allows you to implement a more convenient way to dynamically parse JSON with relative ease.

Dynamic parsing is also very simple:

string jsonString;

JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic responseObject = serializer.Deserialize<object>(jsonString);

responseObject["locations"] = ...
+3
source

All Articles