反序列化嵌套的json在c#中返回null值

时间:2017-03-09 06:43:32

标签: c# json null

我有json对象如下:

我的json如下:

{
    "MTONGUE": "Hindi",
    "REGISTERED": "Y/N",
    "MULTI_PROFILE": "Y/N",
    "PAID": "Y/N",
    "INFO_DTOFBIRTH ": "2010-01-01",
    "INFO_GENDER": "Male/Femal",
    "INFO_MSTATUS": "Married",
    "INFO_RELIGION": "Hindu",
    "RENEWAL": "Y/N",
    "RENEWAL_DAYS": "10",
    "DISCOUNT_TEXT": "Flat/Upto",
    "DISCOUNT_PERCENT": "10",
    "MEMBERSHIP": {
        "P": {
            "P3": {
                "NAME": "eRishta",
                "CALL": "75",
                "DURATION": "3",
                "OFFER_PRICE": "2000"
            },
            "P6": {
                "NAME": "eRishta",
                "CALL": "75",
                "DURATION": "6",
                "OFFER_PRICE": "4000"
            },
            "P12": {
                "NAME": "eRishta",
                "CALL": "75",
                "DURATION": "12",
                "OFFER_PRICE": "6000"
            }
        },
        "C": {
            "C3": {
                "NAME": "eValue",
                "CALL": "75",
                "DURATION": "3",
                "OFFER_PRICE": "2000"
            },
            "C6": {
                "NAME": "eValue",
                "CALL": "75",
                "DURATION": "6",
                "OFFER_PRICE": "4000"
            },
            "C12": {
                "NAME": "eValue",
                "CALL": "75",
                "DURATION": "12",
                "OFFER_PRICE": "6000"
            }
        },
        "NCP": {
            "NCP3": {
                "NAME": "eAdvantage",
                "CALL": "75",
                "DURATION": "3",
                "OFFER_PRICE": "2000"
            },
            "NCP6": {
                "NAME": "eAdvantage",
                "CALL": "75",
                "DURATION": "6",
                "OFFER_PRICE": "4000"
            },
            "NCP12": {
                "NAME": "eAdvantage",
                "CALL": "75",
                "DURATION": "12",
                "OFFER_PRICE": "6000"
            }
        }
    }
}

我需要知道这个json的相关类结构。

我有类似的C#类

 public class results
        {
            public string MTONGUE { get; set; }
            public string REGISTERED { get; set; }
            public string MULTI_PROFILE { get; set; }
            public string PAID { get; set; }
            public string INFO_DTOFBIRTH { get; set; }
            public string INFO_GENDER { get; set; }
            public string INFO_MSTATUS { get; set; }
            public string INFO_RELIGION { get; set; }
            public string RENEWAL { get; set; }
            public string RENEWAL_DAYS { get; set; }
            public string DISCOUNT_TEXT { get; set; }
            public string DISCOUNT_PERCENT { get; set; }
            public List<P> P { get; set; }
        }
        public class P
        {
           public Dictionary<string, List<PC>> PC { get; set; }
        }
        public class PC
        {
            public string NAME { get; set; }
            public string Call { get; set; }
            public string DURATION { get; set; }
            public string OFFER_PRICE { get; set; }
        }

我尝试将反序列化为:

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        results obj = serializer.Deserialize<results>(json);

但嵌套1级的所有内容都返回null值。为什么嵌套的Json会返回null值?

2 个答案:

答案 0 :(得分:0)

你不需要P级。

变化

public List<P> P { get; set; }

public Dictionary<string,Dictionary<string,PC>> MEMBERSHIP { get; set; }

你可以这样访问..

results obj = serializer.Deserialize<results>(str);
var p = obj.MEMBERSHIP["P"]["P12"].DURATION;
var c = obj.MEMBERSHIP["C"]["C12"].DURATION;
var npc = obj.MEMBERSHIP["NCP"]["NCP12"].DURATION;

答案 1 :(得分:0)

首先,您的JSON无效。请找到有效的JSON

{
    "MTONGUE": "Hindi",
    "REGISTERED": "Y\/N",
    "MULTI_PROFILE": "Y\/N",
    "PAID": "Y\/N",
    "INFO_DTOFBIRTH ": "2010-01-01",
    "INFO_GENDER": "Male\/Femal",
    "INFO_MSTATUS": "Married",
    "INFO_RELIGION": "Hindu",
    "RENEWAL": "Y\/N",
    "RENEWAL_DAYS": "10",
    "DISCOUNT_TEXT": "Flat\/Upto",
    "DISCOUNT_PERCENT": "10",
    "MEMBERSHIP": {
        "P": {
            "P3": {
                "NAME": "eRishta",
                "CALL": "75",
                "DURATION": "3",
                "OFFER_PRICE": "2000"
            },
            "P6": {
                "NAME": "eRishta",
                "CALL": "75",
                "DURATION": "6",
                "OFFER_PRICE": "4000"
            },
            "P12": {
                "NAME": "eRishta",
                "CALL": "75",
                "DURATION": "12",
                "OFFER_PRICE": "6000"
            }
        },
        "C3": {
            "NAME": "eValue",
            "CALL": "75",
            "DURATION": "3",
            "OFFER_PRICE": "2000"
        },
        "C6": {
            "NAME": "eValue",
            "CALL": "75",
            "DURATION": "6",
            "OFFER_PRICE": "4000"
        },
        "C12": {
            "NAME": "eValue",
            "CALL": "75",
            "DURATION": "12",
            "OFFER_PRICE": "6000"
        }
    },
    "NCP3": {
        "NAME": "eAdvantage",
        "CALL": "75",
        "DURATION": "3",
        "OFFER_PRICE": "2000"
    },
    "NCP6": {
        "NAME": "eAdvantage",
        "CALL": "75",
        "DURATION": "6",
        "OFFER_PRICE": "4000"
    },
    "NCP12": {
        "NAME": "eAdvantage",
        "CALL": "75",
        "DURATION": "12",
        "OFFER_PRICE": "6000"
    }


}

JSON的C#类

public class P3
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class P6
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class P12
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class P
{
    public P3 P3 { get; set; }
    public P6 P6 { get; set; }
    public P12 P12 { get; set; }
}

public class C3
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class C6
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class C12
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class MEMBERSHIP
{
    public P P { get; set; }
    public C3 C3 { get; set; }
    public C6 C6 { get; set; }
    public C12 C12 { get; set; }
}

public class NCP3
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class NCP6
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class NCP12
{
    public string NAME { get; set; }
    public string CALL { get; set; }
    public string DURATION { get; set; }
    public string OFFER_PRICE { get; set; }
}

public class RootObject
{
    public string MTONGUE { get; set; }
    public string REGISTERED { get; set; }
    public string MULTI_PROFILE { get; set; }
    public string PAID { get; set; }
    public string __invalid_name__INFO_DTOFBIRTH  { get; set; }
    public string INFO_GENDER { get; set; }
    public string INFO_MSTATUS { get; set; }
    public string INFO_RELIGION { get; set; }
    public string RENEWAL { get; set; }
    public string RENEWAL_DAYS { get; set; }
    public string DISCOUNT_TEXT { get; set; }
    public string DISCOUNT_PERCENT { get; set; }
    public MEMBERSHIP MEMBERSHIP { get; set; }
    public NCP3 NCP3 { get; set; }
    public NCP6 NCP6 { get; set; }
    public NCP12 NCP12 { get; set; }
}