简体   繁体   中英

What will be equivalent C# class for this JSON?

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.

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:

我的例子

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM