.NET json序列化 字符串转对象

如图:
josn示例:

{
    "words_result": [
        {
            "words": "處方Prescription"
        },
        {
            "words": "全蠍"
        },
        {
            "words": "0.2克"
        },
        {
            "words": "蜈蚣"
        },
        {
            "words": "0.2克"
        },
        {
            "words": "土鱉蟲"
        },
        {
            "words": "1.2克"
        },
        {
            "words": "彌猴梨根"
        },
        {
            "words": "4.0克"
        },
        {
            "words": "半枝蓮"
        },
        {
            "words": "4.0克"
        },
        {
            "words": "石上柏"
        },
        {
            "words": "4.0克"
        },
        {
            "words": "桃仁"
        },
        {
            "words": "2.0克"
        },
        {
            "words": "赤芍"
        },
        {
            "words": "4.0克"
        },
        {
            "words": "生薑"
        },
        {
            "words": "2.0克"
        },
        {
            "words": "大棗"
        },
        {
            "words": "3.0克"
        },
        {
            "words": "雞內金"
        },
        {
            "words": "2.0克"
        },
        {
            "words": "桑枝"
        },
        {
            "words": "3.0克"
        },
        {
            "words": "桂枝"
        },
        {
            "words": "2.0克"
        },
        {
            "words": "薏苡仁"
        },
        {
            "words": "3.0克"
        },
        {
            "words": "茯苓"
        },
        {
            "words": "3.0克"
        },
        {
            "words": "土茯苓"
        },
        {
            "words": "2.0克"
        }
    ],
    "words_result_num": 33,
    "log_id": 1651491654901787600
}

解析代码:

			string msg = GetStr();
            JObject obj = JObject.Parse(msg);

            // 获取 words_result 数组
            JArray wordsResult = (JArray)obj["words_result"];

            string s = "";
            // 遍历数组,获取每个元素的 words 属性值
            foreach (JObject item in wordsResult)
            {
                string words = (string)item["words"];
                s += words + "\r\n";
            }

用到的库是:Newtonsoft.Json;
在这个示例中,我们首先将 JSON 字符串转换为 JObject 对象,然后通过 [] 操作符获取 words_result 属性的值,得到一个 JArray 对象。接着我们遍历这个数组,对于每个元素都可以通过 [] 操作符获取它的 words 属性的值。

测试图:
.NET json序列化 字符串转对象_第1张图片

你可能感兴趣的:(json,.net,html)