0

JSON:

[
    {
        "ProspectNo": "1000000073",
        "MakerId": "C136771",
        "MkrDate": "2015/11/26",
        "TranID": null,
        "ScoreData": {
            "Client Experience": "1",
            "Stability & Ownership": "7",
            "Property ownership": "22",
            "Co-app/Guarantor": "16",
            "proposed cost": "1800000"
        }
    }
]

This is my attempted class:

[DataContract]
public class Score
{
    [DataMember()]
    public string ProspectNo;

    [DataMember()]
    public string MakerId;

    [DataMember()]
    public string MkrDate;

    //[DataMember()]
    //public string ScoreData;
    [DataMember()]
    //public Dictionary<string, string> ScoreData { get; set; }
    public List<ScoreDataClass> ScoreData;

    [DataMember()]
    public string TranID;
}

[DataContract]
public class ScoreDataClass
{
    [DataMember()]
    public string key { get; set; }
    [DataMember()]
    public string value { get; set; }
}

The problem is that in my method :

public Main CalculateScore(Score scoreobj)
{
    //ScoreData count is always 0. 
}

Everything is fine except that I never get values in ScoreData.

I tried various Json to C# class generators available online such as http://jsonutils.com/ but it did not give desired results.

The JSON string will be sent from client as httppost. I can not change the way it is being sent.

NOTE: I can not hard code it as (Name="Client Experience") etc since I do not know how many or what its going be. It is not necessary that its always going to be Client Experience it could be anything.

SamuraiJack
  • 5,131
  • 15
  • 89
  • 195

2 Answers2

2

JSON objects can be translated into Dictionary<string, object>, but not a list of your custom class with key and value properties.

I have used Dictionary<string, string>, because it seems your object has string values only.
You can use the following class definition:

public class Score
{
    public string ProspectNo { get; set; }
    public string MakerId { get; set; }
    public string MkrDate { get; set; }
    public string TranID { get; set; }
    public Dictionary<string, string> ScoreData { get; set; }
}

That's how you should serialize it:

var result = JsonConvert.DeserializeObject<Score[]>(jsonString);

Note that it is Score[], but not Score, because your JSON represents an array of Score objects.

After serialization, you will be able to access this dictionary like this:

result[0].ScoreData["Stability & Ownership"] // 7

Result of execution:

My example

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
1

Given this is a WCF REST service, you can use the [DataMember] attribute to rename the properties of the generated classes.

public class ScoreData
{
    [DataMember(Name="Client Experience")]
    public string ClientExperience { get; set; }

    [DataMember(Name="Stability & Ownership")]
    public string StabilityOwnership { get; set; }

    [DataMember(Name="Property ownership")]
    public string PropertyOwnership { get; set; }

    [DataMember(Name="Co-app/Guarantor")]
    public string CoAppGuarantor { get; set; }

    [DataMember(Name="proposed cost")]
    public string ProposedCost { get; set; }
}

public class Example
{
    public string ProspectNo { get; set; }
    public string MakerId { get; set; }
    public string MkrDate { get; set; }
    public object TranID { get; set; }
    public ScoreData ScoreData { get; set; }
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I can not hard code it as `(Name="Client Experience")` etc since I do not know how many or what its going be. It is not necessary that its always going to be `Client Experience` it could be anything. – SamuraiJack Dec 15 '15 at 11:36
  • 1
    The see [Generic WCF JSON Deserialization](http://stackoverflow.com/questions/2297903/generic-wcf-json-deserialization) to create a dictionary of your `ScoreData`. – CodeCaster Dec 15 '15 at 11:48