1. 程式人生 > >C#處理json字串

C#處理json字串

這裡用到了一個比較方便的程式包Newtonsoft

程式管理器控制檯中輸入命令:Install-Package Newtonsoft.Json即可

引用:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

 

        static void testHttp()
        {
            var postUrl = "http://yoururl";
            Dictionary<string, string> postData = new Dictionary<string, string>();

            postData.Add("userID", "100000048");
            postData.Add("gradeID", "1");

            // 自己實現的http請求
            string postStr = HttpRequestHelper.DictoryToGetParam(postData);

            var repose = HttpRequestHelper.HttpPostRequest(postUrl, postStr);

            Console.WriteLine(repose + "\r\n");


            object product = JsonConvert.DeserializeObject(repose);
            Console.WriteLine(product);

            var jo = JObject.Parse(repose);

            foreach (var m in jo)
            {
                Console.WriteLine("key : " + m.Key);
                Console.WriteLine(m);
                Console.WriteLine(m.Value);
            }

            Console.WriteLine(jo.Value<object>("returnResult"));

            JObject jo2 = (JObject)JsonConvert.DeserializeObject(repose);
            Console.WriteLine(Convert.ToString(jo2["returnResult"]["abc"]));//Convert.ToString(null)不會丟擲異常,null.ToString()會丟擲異常
            Console.WriteLine(Convert.ToString(jo2["returnResult"]["activity_name"]));

            Console.ReadLine();
        }

結果:

{"returnResult":{"school_name":"\u8d85\u7ea7\u8f85\u5bfc\u6700\u5f3a\u5bb6\u957f\u7fa4",
"grades":[{"grade_id":"1","school_id":"1","grade_des":"\u521d\u4e2d","grade_doc":"",
"create_time":null,"grade_qrcode_img":null},
{"grade_id":"2","school_id":"1","grade_des":"\u9ad8\u4e2d","grade_doc":"",
"create_time":null,"grade_qrcode_img":null}]},"statusCode":0,"errorMsg":""}


{
  "returnResult": {
    "school_name": "希望小學",
    "grades": [
      {
        "grade_id": "1",
        "school_id": "1",
        "grade_des": "初中",
        "grade_doc": "",
        "create_time": null,
        "grade_qrcode_img": null
      },
      {
        "grade_id": "2",
        "school_id": "1",
        "grade_des": "高中",
        "grade_doc": "",
        "create_time": null,
        "grade_qrcode_img": null
      }
    ]
  },
  "statusCode": 0,
  "errorMsg": ""
}
key : returnResult
[returnResult, {
  "school_name": "希望小學",
  "grades": [
    {
      "grade_id": "1",
      "school_id": "1",
      "grade_des": "初中",
      "grade_doc": "",
      "create_time": null,
      "grade_qrcode_img": null
    },
    {
      "grade_id": "2",
      "school_id": "1",
      "grade_des": "高中",
      "grade_doc": "",
      "create_time": null,
      "grade_qrcode_img": null
    }
  ]
}]
{
  "school_name": "希望小學",
  "grades": [
    {
      "grade_id": "1",
      "school_id": "1",
      "grade_des": "初中",
      "grade_doc": "",
      "create_time": null,
      "grade_qrcode_img": null
    },
    {
      "grade_id": "2",
      "school_id": "1",
      "grade_des": "高中",
      "grade_doc": "",
      "create_time": null,
      "grade_qrcode_img": null
    }
  ]
}
key : statusCode
[statusCode, 0]
0
key : errorMsg
[errorMsg, ]

{
  "school_name": "希望小學",
  "grades": [
    {
      "grade_id": "1",
      "school_id": "1",
      "grade_des": "初中",
      "grade_doc": "",
      "create_time": null,
      "grade_qrcode_img": null
    },
    {
      "grade_id": "2",
      "school_id": "1",
      "grade_des": "高中",
      "grade_doc": "",
      "create_time": null,
      "grade_qrcode_img": null
    }
  ]
}

希望小學

結論

JsonConvert.DeserializeObject(repose);解析json字串,php後臺返回的json格式的字串

var jo = JObject.Parse(repose);返回JObject型別資料,JObject可以通過["property_name"]獲取property_name對應的資料

這是資料又是一個JObject型別資料,如此迴圈往復

            foreach (var m in jo)
            {
                Console.WriteLine("key : " + m.Key);
                Console.WriteLine(m);
                Console.WriteLine(m.Value);
            }

            Console.WriteLine(jo.Value<object>("returnResult"));

 

好用的方法

            JObject jo2 = (JObject)JsonConvert.DeserializeObject(repose);
            Console.WriteLine(Convert.ToString(jo2["returnResult"]["abc"]));//Convert.ToString(null)不會丟擲異常,null.ToString()會丟擲異常
            Console.WriteLine(Convert.ToString(jo2["returnResult"]["activity_name"]));

迴圈往復的使用["property_name"]["property_name"]["property_name"]獲取屬性值,如果是陣列,可以用[0]取值,繼續["property_name"][0]["property_name"]取值