3

So i'm getting a response like this

{"$id":"1","success":true,"errors":{"$id":"2","$values":[]}}

how can i convert this into to a c# object, tried using this(http://json2csharp.com/) tool to make an output but it doesn't make sense this is what i'm getting

x

public class Errors
{
    public string __invalid_name__$id { get; set; }
    public List<object> __invalid_name__$values { get; set; }
}

public class RootObject
{
    public string __invalid_name__$id { get; set; }
    public bool success { get; set; }
    public Errors errors { get; set; }
}

I'm kinda new to c#, any inputs would be deeply appreciated, i basically need access to success key variable

Sam
  • 67
  • 8

4 Answers4

6

You need to add [JsonProperty] attribute to every property that key name started with dollar $

public class Errors
{
    [JsonProperty("$id")]
    public string id { get; set; }

    [JsonProperty("$values")]
    public List<object> values { get; set; }
}

public class RootObject
{
    [JsonProperty("$id")]
    public string id { get; set; }
    public bool success { get; set; }
    public Errors errors { get; set; }
}

Because the $ indicates metadata, not an actual data field. so you have to modify your JsonSerializerSettings to ignore MetadataPropertyHandling.

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

And finally deserialize your json to above class objects.

RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json, settings);

Here I created a sample console app for demonstration purpose that shows how above code will work.

class program
{
    public static void Main()
    {
        string json = File.ReadAllText(@"Path to your json file");

        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

        RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json, settings);

        Console.WriteLine("id: " + rootObject.id);
        Console.WriteLine("success: " + rootObject.success);
        Console.WriteLine("errors.id: " + rootObject.errors.id);
        Console.WriteLine("errors.values: " + string.Join(",", rootObject.errors.values));
        Console.ReadLine();
    }
}

Output:

enter image description here

er-sho
  • 9,581
  • 2
  • 13
  • 26
1

Well, What you can do is

public class Errors
{
    [JsonProperty(PropertyName = "$id")]
    public string id { get; set; }
    [JsonProperty(PropertyName = "$values")]
    public List<object> values { get; set; }
}

public class RootObject
{
    [JsonProperty(PropertyName = "$id")]
    public string id { get; set; }
    public bool success { get; set; }
    public Errors errors { get; set; }
}
zetawars
  • 1,023
  • 1
  • 12
  • 27
0

You need your object attributes to match you json string ($id instead of _invalid_name_$id), then you can use:

JsonConvert.DeserializeObject<RootObject>(jsonString);
aqteifan
  • 456
  • 1
  • 5
  • 17
0

Here is a simple class to serialize json string from object or to object (T). May de/serialize array(list) of objects.

public class HelperSerializer<T> where T: class
{ 
    public static string WriteFromObject(T source)
    {
        using (var ms = new MemoryStream())            {  
            var ser = new DataContractJsonSerializer(typeof(T));
            ser.WriteObject(ms, source);
            byte[] json = ms.ToArray();
            return Encoding.UTF8.GetString(json, 0, json.Length);
        }
    }

    // Deserialize a JSON stream to an object.  
    public static T ReadToObject(string json)
    {          

        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            var ser = new DataContractJsonSerializer(typeof(T));
            return ser.ReadObject(ms) as T;
        }
    }

}

Use persons = HelperSerializer<List<Person>>.ReadToObject(json); and var json = HelperSerializer<List<Person>>.WriteFromObject(persons);

Lapenkov Vladimir
  • 3,066
  • 5
  • 26
  • 37