我如何序列化Windows手机的json响应?

时间:2015-11-02 09:31:51

标签: c# json windows-phone-8 serialization

我正在关注Json回复

 string Response=  "" {
"category": [
    {
        "biller": [
            {
                "adhocPayment": "Y",
                "billPresentment": "N",
                "billerCategoryId": "05",
                "billerId": "BILLPAYTEST",
                "billerLogo": "#",
                "billerName": "Airtel Chennai",
                "displayLogoName": "#",
                "label1": "CONSUMER NO",
                "label2": "PHONE NO",
                "maxAmount": "10000",
                "minAmount": "10"
            },
            {
                "adhocPayment": "Y",
                "billPresentment": "N",
                "billerCategoryId": "05",
                "billerId": "VODFONE",
                "billerLogo": "#",
                "billerName": "VODOFONE",
                "displayLogoName": "#",
                "label1": "CONSUMER NO",
                "label2": "PHONE NO",
                "maxAmount": "10000",
                "minAmount": "10"
            }
        ],
        "categoryName": "Mobile Recharge",
        "totalRecords": 2
    },
    {
        "biller": [
            {
                "adhocPayment": "N",
                "billPresentment": "N",
                "billerCategoryId": "30",
                "billerId": "SUNTVDTH",
                "billerLogo": "#",
                "billerName": "SUN TV",
                "displayLogoName": "#",
                "label1": "Subscriber ID^10^10^N^N^N",
                "label2": "#",
                "maxAmount": "10000",
                "minAmount": "100"
            }
        ],
        "categoryName": "DTH Recharge",
        "totalRecords": 1
    }
],
"dcid": "01",
"startRecNo": "0001",
"totalRecord": 0,
"txnno": "0211151051285128"
}""

序列化我正在关注

附上课程

 public class RootObject
    {

        public List<Category> category { get; set; }
        public string dcid { get; set; }
        public object startRecNo { get; set; }
        public int totalRecord { get; set; }
        public string txnno { get; set; }
    }

    public class Category
    {
        public List<Biller> biller { get; set; }
        public string categoryName { get; set; }
        public int totalRecords { get; set; }
    }

    public class Biller
    {

        public string adhocPayment { get; set; }
        public string bankCode { get; set; }
        public string billPresentment { get; set; }
        public string billerCategoryId { get; set; }
        public string billerId { get; set; }
        public string billerLogo { get; set; }
        public string billerMnemoName { get; set; }
        public string billerName { get; set; }
        public string delChannelId { get; set; }
        public string label1 { get; set; }
        public string label2 { get; set; }
        public string label3 { get; set; }
        public string label4 { get; set; }
        public string label5 { get; set; }
        public string labelType { get; set; }
        public string maxAmount { get; set; }
        public string minAmount { get; set; }
        public string minimumPayment { get; set; }
        public string recordCount { get; set; }
    }

在按钮单击事件处理程序中,像这样序列化

foreach (var nit in rootelement.category)
        {

            for (int i = 1; i < nit.biller.Count; i++)
            {
              string b = nit.biller[i].billerName.ToString();
              string c=nit.biller[i].label1.ToString();
            }
        }

我的要求是有两个标签(label1,label2)作为响应..基于我将在设计中创建两个文本框并显示它们。

响应中的标签数量是动态的, 每次更改时,如何从响应中检查/获取标签计数。 基于我需要在设计中显示相同的文本框..

请帮我解决这个问题..

2 个答案:

答案 0 :(得分:0)

dynamic collectionWrapper = new
{
    ppp = new RootObject()
    {
        dcid = "01",
        startRecNo = "0001",
        totalRecord = 0,
        txnno = "0211151051285128",
        category = new List<Category> 
        {
            new Category()
            {
                categoryName = "Mobile Recharge",
                totalRecords = 2,
                biller = new List<Biller>
                {
                    new Biller()
                    {                                   
                        adhocPayment="Y",
                        billPresentment="N",
                        billerCategoryId="05",
                        billerId="BILLPAYTEST",
                        billerLogo="#",
                        billerName= "Airtel Chennai",
                        displayLogoName= "#",
                        label1= "CONSUMER NO",
                        label2= "PHONE NO",
                        maxAmount= "10000",
                        minAmount= "10"
                    },
                    ......................
                }
            },
            new Category()
            {
                categoryName = "Mobile Recharge",
                totalRecords = 2,
                biller = new List<Biller>
                {
                    new Biller()
                    {                                   
                        adhocPayment="Y",
                        billPresentment="N",
                        billerCategoryId="05",
                        billerId="BILLPAYTEST",
                        billerLogo="#",
                        billerName= "Airtel Chennai",
                        displayLogoName= "#",
                        label1= "CONSUMER NO",
                        label2= "PHONE NO",
                        maxAmount= "10000",
                        minAmount= "10"
                    }
                }
            },
            ..................... 
        }
    }
};

var output = JsonConvert.SerializeObject(collectionWrapper);

答案 1 :(得分:0)

您可以直接使用JSON.NET反序列化为json并从JSON反序列化。请避免这样做&#34;手工&#34;。您可以在一行代码中转换您的任务:

string output = JsonConvert.SerializeObject(product);

您需要的文档是available here

您可以将其作为Nuget package安装在项目中。

实际上,您应该使用Web API,它允许您以非常简单的方式使用HTTP和JSON序列化在设备和服务器之间进行通信。