How do deserialize this JSON into an object?

前端 未结 5 706
醉酒成梦
醉酒成梦 2021-01-14 02:36

I\'m trying to use JSON.Net to deserialize a JSON object into a C# object.

The object I want to create is MonthlyPerformance which contains a list of

5条回答
  •  长发绾君心
    2021-01-14 03:26

    I believe the problem is in your Json string.

    "Type": [ ... ] 
    

    Should be

    "Types": [ ... ]  
    

    Types is the name of property that should be deserialized, you mistakenly put Type the class name instead.

    The same goes for Categories property in the Type class.

    Also I simply removed "MonthlyPerformance" root from the Json string and it worked like a charm. With Json.NET of course.

    Here is a snippets of the modified Json with appropriate property names (notice, Types, Categories, Funds and the absence of MonthlyPerformance root)

    {
        "Types": [ 
        { 
        "id": "65", 
        "countryId": "IE", 
        "name": "Irish Domestic Funds (Gross)", 
        "Categories": [ 
            { 
            "id": "25003334", 
            "countryId": "IE", 
            "name": "UK Equity", 
            "Funds": [ 
                { 
                "id": "25000301", 
                "countryId": "IE", 
                "name": "Aviva Irl UK Equity Fund" 
                }, 
                { 
                "id": "25000349", 
                "countryId": "IE", 
                "name": "New Ireland UK Equity 9" 
            } 
        ] 
    }
    

提交回复
热议问题