码迷,mamicode.com
首页 > Windows程序 > 详细

C#解析Json字符串

时间:2016-07-13 00:55:13      阅读:718      评论:0      收藏:0      [点我收藏+]

标签:

1.
var resultStr={"from":"en","to":"zh","trans_result":[{"src":"apple","dst":"\u82f9\u679c"}]}
            var data = JObject.Parse(resultStr);
            if (data.Property("trans_result")!=null)
            {
                var trans_result = data.Property("trans_result").Value.ToString();
                var trans = (JArray) JsonConvert.DeserializeObject(trans_result);
                var dst = trans[0]["dst"];
                var src = trans[0]["src"];
                var fromStr = data.Property("from").Value.ToString();
                var tttt = data.Property("to").Value.ToString();
                var from = data.Property("from").Value.ToString() == "\"zh\"" ? "中文" : "英文";
                var toStr = data.Property("to").Value.ToString() == "\"zh\"" ? "中文" : "英文";
                result = string.Format("将{0}从{1}翻译到{2}的意思是:{3},", src.ToString(), from, toStr, dst);
            }


2.            var obj = JsonConvert.DeserializeObject<JObject>(data);//PuName
            var dataStr = obj.Value<string>("data").Replace("\\\"", "\"").Replace("\\n", " ").Replace("\\t", " ").Replace("\\", " ").Replace("\" \"", "\"").Replace("\"\"黄", "\"黄").Replace("\"   \"", "\"");
            var jsonObj = JsonConvert.DeserializeObject<JObject>(dataStr);
            var rowstr = "{" + jsonObj.Property("rows").ToString() + "}";
            JObject rowList = JObject.Parse(rowstr);
            foreach (var  r in rowList["rows"])
            {
                var dy = JsonConvert.DeserializeObject<JObject>(r.ToString());
                var PuName = dy.Value<string>("PuName");
                PuName = dy["PuName"].ToString();
                PuName = dy.Property("PuName").Value.ToString();
            }

C#解析Json字符串

标签:

原文地址:http://www.cnblogs.com/change4now/p/5665224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!