将JSON转换为具有混合值的c sharp字典

时间:2016-12-05 12:46:48

标签: c# json dictionary serialization

有没有人对如何将具有混合单值和数组值的json字典转换为C#具体对象有任何建议。字典int,动态不起作用,见下面的json。

{
    "outcome": "SUCCESS",
    "response": {
        "249998": "1203088",
        "249999": "1203091",
        "250000": "1203093",
        "250001": "1203095",
        "250003": "1203096",
        "250004": "1203102",
        "250005": "1203107",
        "250006": {
            "1203109": 1,
            "1203110": 0,
            "1203111": 0,
            "1203112": 0,
            "1203113": 0,
            "1203114": 0,
            "1203115": 0,
            "1203116": 0,
            "1203117": 0,
            "1203118": 0,
            "1203119": 0,
            "1203120": 0
        },
        "250007": {
            "1203121": 0,
            "1203122": 0,
            "1203123": 0,
            "1203124": 0,
            "1203125": 1,
            "1203126": 0,
            "1203127": 0,
            "1203128": 0
        },
        "250008": {
            "1203129": "fcdvgbjkl;"
        },
        "250010": "1203130",
        "250011": "1203135",
        "250012": "1203140",
        "250013": "1203145",
        "250014": "1203150",
        "250015": "1203155",
        "250017": "1203160",
        "250018": "1203165",
        "250019": "1203170",
        "250020": "1203175",
        "250021": "1203181",
        "250023": "1203183",
        "250024": "1203188",
        "250025": "1203193",
        "250026": "1203198",
        "250027": "1203203",
        "250028": "1203208",
        "250029": "1203213",
        "250030": "1203219",
        "250031": {
            "1203221": 0,
            "1203222": 0,
            "1203223": 0,
            "1203224": 0,
            "1203225": 1,
            "1203226": 0,
            "1203231": 0
        },
        "250036": {
            "1203236": 0,
            "1203237": 0,
            "1203238": 0,
            "1203243": 0,
            "1203244": 0,
            "1203245": 0,
            "1203246": 1,
            "1203247": 0,
            "1203248": 0,
            "1203249": 0,
            "1203250": 0,
            "1203251": 0,
            "1203252": 0,
            "1203253": 0,
            "1203254": 0,
            "1203255": 0,
            "1203256": 0,
            "1203257": 0,
            "1203258": 0,
            "1203259": 0
        },
        "250041": {
            "1203264": "gjhgjhg"
        },
        "250045": {
            "1203269": 0
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用object作为字典的值,该字典将接受字符串或整数。您需要一个多级对象,如下面的代码

public class MyClass
    {
        Dictionary<int, int> dict = new Dictionary<int, int>();
        Dictionary<int, object> childDict = new Dictionary<int, object>();
    }
相关问题