在类c#中存储JSON响应

时间:2017-06-27 07:27:57

标签: c# json

我有一个Json,我正在解析并尝试使用JavaScriptSerializer()存储在一个类中。反序列化(json);这样我就可以通过直接调用该类来使用响应。

以下是代码,

 protected void GetDetails_Click(object sender, EventArgs e)
    {
        var webClient = new WebClient();
        string json = webClient.DownloadString(@"http://localhost/test.json?" + mobtxtb.Text);
        var jsonObj = new JavaScriptSerializer().Deserialize<Root>(json);
        Label1.Text = "RE Response Status :: " + jsonObj.statusCode.ToString();
        Label2.Text = "RE Status Deacription :: " + jsonObj.statusDesc.ToString();
        Label3.Text = "RecoID :: " + jsonObj.Reco.RecoID;
        Label[] rank = new Label[jsonObj.Reco.Channels.Count + 1];
        Label[] name = new Label[jsonObj.Reco.Channels.Count + 1];
        foreach (Channels c in jsonObj.Reco.Channels)
        {
            rank[c.rank] = new Label();
            name[c.rank] = new Label();
            rank[c.rank].Text = "Channel Priority :: " + c.rank.ToString();
            name[c.rank].Text = "Channel :: " + c.name.ToString();
            this.Controls.Add(new LiteralControl("<br />"));
            this.Controls.Add(rank[c.rank]);
            this.Controls.Add(new LiteralControl("<br />"));
            this.Controls.Add(name[c.rank]);
            string z = c.layout.ToString();
            foreach (layout l in c.layout)
            {

            }
            this.Controls.Add(new LiteralControl("<br />"));
        }
    }


public class Root
{
    public string statusDesc { get; set; }
    public reco Reco { get; set; }
    public int statusCode { get; set; }
}
public class birthday
{
    public List<string> bday { get; set; }
}
public class reco
{        
    public List<Channels> Channels  { get; set; }
    public int ClusterID { get; set; }
    public string RecoID { get; set; }
    public List<Carousel> carousel { get; set; }
    public List<birthday> birthday { get; set; }
}
public class Carousel
{
    public List<SUB> CarouselList { get; set; }
}
public class Channels
{
    public string name { get; set; }
    public int rank { get; set; }
    public List<layout> layout { get; set; }
}

public class layout
{
    public int id { get; set; }
    public c1 c1 { get; set; }
    public c2 c2 { get; set; }
    public c3 c3 { get; set; }
}

public class c1
{
    public PPD PPD { get; set; }
}
public class c2
{
    public SUB SUB { get; set; }
}
public class c3
{
    public VP VP { get; set; }
}

public class SUB
{
    public string eventID { get; set; }
    public string sname { get; set; }
    public string bid { get; set; }
    public string sid { get; set; }
    public string cid { get; set; }
}

public class PPD
{
    public string cid { get; set; }
}

public class VP
{
    public string cid { get; set; }
}

这是我需要解析的JSON。

{
   "statusDesc":"Success",
   "reco":{
      "birthday":[
         {
            "birthday":"Y"
         }
      ],
       "channels":[
         {
            "layout":{
               "c3":{
                  "PPD":{
                      "CID":36256
                  }
                },
                "id":3,
                "c1":{
                 "SUB":{
                      "eventID":"MOON_VID0021_007_VUC01486203_A",
                      "sname":"SunCorp Mobile Box Office",
                      "bid":"33764",
                      "sid":"YOYO_WEEKLYMOVIES",
                      "CID":"MOVIES_W"
                    }
               },
               "c2":{
                  "VP":{
                     "CID":35971
                  }
               }
            },
             "name":"Videos",
             "rank":1
     },
     {
        "layout":{
           "id":2,
           "c1":{
              "VP":{
                 "CID":36044
              }
           }
        },
        "name":"Music",
        "rank":2
     },
     {
        "layout":{
           "id":1,
           "c1":{
              "VP":{
                 "CID":33617
              }
           }
        },
        "name":"Images",
        "rank":3
     },
     {
        "layout":{
           "id":3,
           "c1":{
              "SUB":{
                 "sname":"SunCorp TV "
              }
           }
        },
        "name":"Live TV",
        "rank":4
     },
     {
        "layout":{
           "id":2,
           "c1":{
              "VP":{
                 "CID":34410
              }
           }
        },
        "name":"Games",
        "rank":5
     },
     {
        "layout":{
           "id":1,
           "c1":{
              "SUB":{
                 "eventID":"MOON_HLT0001_001_MED01484002_F",
                 "sname":"Health",
                 "bid":"33420",
                 "sid":"SEA_HEALTH",
                 "CID":"MHEALTH"
              }
           }
        },
        "name":"Health",
        "rank":6
     },
     {
        "layout":{
           "id":3,
           "c1":{
              "PPD":{
                 "CID":22
              }
           }
        },
        "name":"News",
        "rank":7
     },
     {
        "layout":{
           "id":2,
           "c1":{
              "PPD":{
                 "CID":12080
              }
           }
        },
        "name":"Astrology",
        "rank":8
     },
     {
        "layout":{
           "id":1,
           "c1":{
              "VP":{
                 "CID":34464
              }
           }
        },
        "name":"Devotional",
        "rank":9
     },
     {
        "layout":{
           "id":3,
           "c1":{
              "PPD":{
                 "CID":12033
              }
           }
        },
        "name":"Coupons",
        "rank":10
     },
     {
        "layout":{
           "id":3,
           "c1":{
              "PPD":{
                 "CID":34465
              }
           }
        },
        "name":"Caller Tunes",
        "rank":11
     },
     {
        "layout":{
           "id":3,
           "c1":{
              "PPD":{
                 "CID":18
              }
           }
        },
        "name":"Social",
        "rank":12
     }
  ],
  "clusterID":1835,
  "recoID":49785146390432821,
  "carousel":[
     {
        "SUB":{
           "eventID":"MOON_GAM0030_007_IND01482006_A",
           "sname":"SunCorp Fun Zone",
           "bid":12257,
           "sid":"IG_FUNZONEW",
           "CID":"IG_FUNZONEW"
        },
        "rank":1
     },
     {
        "SUB":{
           "eventID":"MOON_CST0035_007_HUN01488011_A",
           "sname":"Re. 1 Store Service",
           "bid":12146,
           "sid":"HNG_STOREW",
           "CID":"STORE_W"
        },
        "rank":2
     },
     {
        "SUB":{
           "eventID":"MOON_VTV0150_030_APA01504003_A",
           "sname":"SunCorp TV ",
           "bid":35544,
           "sid":"TV_ALLCHANNEL_M",
           "CID":"ALLCHANNEL_M"
        },
        "rank":3
     }
  ]

},         &#34;的StatusCode&#34;:0      }

此代码适用于布局,但在此之后无法解析

1 个答案:

答案 0 :(得分:1)

我知道回答为时已晚,但正如@gymni所建议的那样,Newtonsoft lib对我来说非常合适。