C#解析Json字符串

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();
            }

posted @ 2016-07-12 22:48  change_4_now  阅读(411)  评论(0编辑  收藏  举报