0

I have a Jtoken having structure as. The below structure have even more properties than below but I have to use only the below ones

{{ "PersonId": 1234, "State": "Florida", "Gender": "Male", "Married ": 1, "SalaryUnderHundredDollar ": 1, }} 

I have created a class Person as

public class Person
{
  public int PersonId {get;set;}
  public string State {get;set;}
  public string Gender {get;set;}
  public bool Married {get; set}
  public bool SalaryUnderHundredDollar {get;set}
}

I am converting the above Jtoken into dictionary as :

var dict = jtoken.First.ToObject<Dictionary<string, object>>().ToDictionary(x => x.Key, x => x.Value);

This will give all the properties of the Person but I want only the above ones.And now I have to convert

     Dictionary<string,object> to Dictionary<string,class>

I am doing the following:

 var dict2= dict.Where(x => fieldsRequired.Contains(x.Key)).ToDictionary(x => x.Key, x => (Person)x.Value);

fieldsRequired is a list of string fields which I need since in Jtoken there are number of fields.

But this conversion is not working.

Any help?

1 Answers1

1

Modify your class as:

public class Person
{
  public int PersonId {get;set;}
  public string State {get;set;}
  public string Gender {get;set;}
  public int Married {get; set}
  public int SalaryUnderHundredDollar {get;set}
}

And then use:

var person = jtoken.First.ToObject<Person>();

The problem is that bool properties are represented as 0 and 1 in your JSON. So instead we change the type of the properties in your class to int as well.

A better alternative would be to fix the JSON to have true and false instead of 0 and 1. Also it is quite weird the JSON is wrapped in double curly braces and the "Married " and "SalaryUnderHundredDollar " properties contain a space at the end. Those problems should be fixed as well.

Also you can write a custom JsonConverter to convert the values appropriately from 0 and 1 to bool. See answer here.

Putting the suggestions together and fixing the double curly braces and spaces in property names results in this:

public class BoolConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue(((bool)value) ? 1 : 0);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return reader.Value.ToString() == "1";
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(bool);
    }
}

public class Person
{
    public int PersonId { get; set; }
    public string State { get; set; }
    public string Gender { get; set; }
    [JsonConverter(typeof(BoolConverter))]
    public bool Married { get; set; }
    [JsonConverter(typeof(BoolConverter))]
    public bool SalaryUnderHundredDollar { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        string json =
            @"{ ""PersonId"": 1234, ""State"": ""Florida"", ""Gender"": ""Male"", ""Married"": 1, ""SalaryUnderHundredDollar"": 1 }";

        var jObject = JObject.Parse(json);
        var person = jObject.ToObject<Person>();
    }
}
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • Sorry I picked the wrong jtoken and now I am seeing the bool values are true or false but still facing the isue. – newbiecsharp Feb 04 '18 at 09:47
  • Could you post the code which you have? Including the parsing, the correct JSON and so on? The code you are using doesn't require the Dictionary approach at all, you should convert to Person class directly and skip the dictionary completely – Martin Zikmund Feb 04 '18 at 09:49