User987
User987

Reputation: 3825

Converting JSON into C# object classses

I have a JSON which basically looks like this:

{
    "body": 
    {
        "mode": "raw",
        "raw": "{
            \"Token\" : \"123123\", \"queryName\" : \"testMethod\" ,\"dataTestToSEND\" :{\"IDs\":[\"B00448MZUW\",\"B071F7LBN6\",\"B01BBZJZHQ\"],\"Marketplace\":\"southAmerica\",\"Region\":\"West\",\"PricingMethod\":0}} "a
        },
        "description": "some description here"
    }
}

And when I converted it to C# object classes I got this:

public class Body
{
    public string mode { get; set; }
    public string raw { get; set; }
}

public class RootObject
{
    public Body body { get; set; }
    public string description { get; set; }
}

I used json2csharp tool here..

Now what confuses me here is the "raw" property as you can see.. The tool converted it into a string, but this doesn't really looks like a string to me?

Rather a raw, the way I see it, should be an class which contains something like this:

public class Raw
{
    public string Token { get; set; }
    public string queryName { get; set; }
    public List<string//?not sure which type does this needs to be set to?> 
    dataTestToSEND { get; set }
    public string marketplace { get; set; }
    public string Region { get; set }
}

Can someone help me out with this? How can I structure a proper set of classes and objects for this JSON? It's very confusing for me right now...

Upvotes: 0

Views: 194

Answers (3)

hasan
hasan

Reputation: 3502

You can use JSON.NET to convert your json to specific class

Official-site: http://www.newtonsoft.com/json

You can remove backslashes from json to let JObject interpret it.

public class Raw
{
    public Raw(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken jRaw = jObject["raw"];
        Token = (string) jRaw["token"];
        queryName = (string) jRaw["queryName"];
        dataTestToSEND = (List<string>) jRaw["dataTestToSEND"];
        marketplace = (string) jRaw["Marketplace"]
        Region= jRaw["players"].ToArray();
    }

    public string Token {get;set;}
    public string queryName {get;set;}
    public List<string> dataTestToSEND {get;set}
    public string marketplace {get;set;}
    public string Region{get;set}



}

// Call User with your json
string json = @"{""body"":{""mode"":""raw"",""raw"":{""Token"":""123123"",""queryName"":""testMethod"",""dataTestToSEND"":{""IDs"":[""B00448MZUW"",""B071F7LBN6"",""B01BBZJZHQ""],""Marketplace"":""southAmerica"",""Region"":""West"",""PricingMethod"":""0""}}},""description"":""somedescriptionhere""}";
Raw raw = new Raw(json);

Upvotes: 3

Sir Rufo
Sir Rufo

Reputation: 19106

Build a custom converter to convert from a string property to a type

public class RawConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType != JsonToken.String)
        {
            throw new InvalidOperationException();
        }
        var value = (string)reader.Value;
        var obj = JsonConvert.DeserializeObject(value, objectType);
        return obj;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var str = JsonConvert.SerializeObject(value);
        writer.WriteValue(str);
    }
}

and put an attribute on the property where you need that converter

public class Body
{
    public string mode { get; set; }
    // This property is a raw string in the json document
    [JsonConverter(typeof(RawConverter))]
    public Data raw { get; set; }
}

public class RootObject
{
    public Body body { get; set; }
    public string description { get; set; }
}

public class Data
{
    public string Token { get; set; }
    public string queryName { get; set; }
    public DataTestToSEND dataTestToSEND { get; set; }
}

public class DataTestToSEND
{
    public string[] IDs { get; set; }
    public string Marketplace { get; set; }
    public string Region { get; set; }
    public int PricingMethod { get; set; }
}

and now you can deserialize the given json

{
  "body": {
    "mode": "raw",
    "raw": "{\"Token\":\"123123\",\"queryName\":\"testMethod\",\"dataTestToSEND\":{\"IDs\":[\"B00448MZUW\",\"B071F7LBN6\",\"B01BBZJZHQ\"],\"Marketplace\":\"southAmerica\",\"Region\":\"West\",\"PricingMethod\":0}}"
  },
  "description": "some description here"
}

with

var result = JsonConvert.DeserializeObject<RootObject>( jsonString );

Live example on .net fiddle with deserialization and serialization

Upvotes: 2

PMerlet
PMerlet

Reputation: 2594

json2csharp converted your raw property to a string because it is not able to parse correctly JSON documents with escape characters. Remove the escape characters in order to let json2csharp create the right sharp class.

{"body":{"mode":"raw","raw":{"Token":"123123","queryName":"testMethod","dataTestToSEND":{"IDs":["B00448MZUW","B071F7LBN6","B01BBZJZHQ"],"Marketplace":"southAmerica","Region":"West","PricingMethod":0}}},"description":"somedescriptionhere"}

Upvotes: 3

Related Questions