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"]取值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值