C#解析Json形式データ小結

14004 ワード

最近,いくつかの異なるJson形式のデータに遭遇し,得たい結果に変換するには異なる処理が必要であることをまとめた.
1つ目の形式:statusでは{}形式、オブジェクト
string json =

                 @"{'name':'Tom','province':'32','city':'1','location':'     ','status':{'created_at':'Thu Feb 26 21:04:34 +0800 2015','text':'  '}}";

以上のフォーマットのデータに対して、次の2つの構造体またはクラスを作成します.
 1     public struct Status

 2     {

 3         public string created_at { get; set; }

 4         public string text { get; set; }

 5     }

 6 

 7     public struct JsonData

 8     {

 9         public string name { get; set; }

10         public string province { get; set; }

11         public string city { get; set; }

12         public string location { get; set; }

13         public Status status;

14     }

出力結果:
1   JavaScriptSerializer jsSerializer=new JavaScriptSerializer();

2    JsonData jd = jsSerializer.Deserialize<JsonData>(json); 3   Response.Write(string.Format("name={0};province={1};city={2};location={3};status={4};",jd.name,jd.province,jd.city,jd.location,jd.status.created_at+jd.status.text));

 
2つ目の形式:statusでは[]形式、配列
string json =

                @"{'name':'Tom','province':'32','city':'1','location':'     ','status':[{'created_at':'Thu Feb 26 21:04:34 +0800 2015','text':'  '}]}";

以上のフォーマットのデータに対して、次の2つの構造体またはクラスを作成します.
 1   public struct Status

 2     {

 3         public string created_at { get; set; }

 4         public string text { get; set; }

 5     }

 6     public struct JsonData2

 7     {

 8         public string name { get; set; }

 9         public string province { get; set; }

10         public string city { get; set; }

11         public string location { get; set; }

12         public List<Status> status;

13     }

出力結果
1      JavaScriptSerializer jsSerializer=new JavaScriptSerializer();

2      JsonData2 jd = jsSerializer.Deserialize<JsonData2>(json);

3      Response.Write(string.Format("name={0};province={1};city={2};location={3};status={4};",jd.name,jd.province,jd.city,jd.location,jd.status[0].created_at+jd.status[0].text));

 
 
プロジェクトの適用:
json文字列:
    {

    "depart_id": 5,

    "depart_name": "   ",

    "depart_source": "[{\"text\": \"\", \"type\": \"text\"},{\"text\": \"\", \"type\": \"image\"},{\"text\": \"\", \"type\": \"audio\"}]",

    "staff": {

        "name": "  ",

        "title": "    ",

        "image": "/2015/1/13/d2e2e3f2c2f8_2e4f5b.jpg",

        "id": 143375468

        }

     }

クラスを作成するには
 1 public class DepatData

 2 {

 3     public int depart_id = 0;

 4     public string depart_name = "";

 5     public string depart_source = "";

 6     public StaffData staff =new StaffData();

 7 

 8     public class StaffData

 9     {

10         public string name = "";

11         public string title = "";

12         public string image = "";

13         public string id = "";

14     }

15 }

Jsonデータの解析:
 1         DepatData d = JsonConvert.DeserializeObject<DepatData>(strJson);

 2         List<Dictionary<string, string>> depart_source =

 3             JsonConvert.DeserializeObject < List<Dictionary<string, string>>>(d.depart_source);

 4         

 5         //   

 6         int depart_id = d.depart_id;

 7         .......

 8         string text = depart_source[0]["text"];

 9         string type = depart_source[0]["type"];

10         .......