C#处理Json字符串、Json对象、多层嵌套

标签: C# json 对象 asp.net
732人阅读 评论(0) 收藏 举报
分类:

1.C#   

 

public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public Models.Users[] obj { get; set; }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //json
            string strs = "{\"ID\":\"2\",\"UserName\":\"zhonguog\",\"PassWord\":\"125\",\"Nickname\":\"放开那段代码\"}";
            JObject jonObj = JObject.Parse(strs);//引用Newtonsoft.Json.Linq;
            Response.Write(jonObj["UserName"].ToString() + "------" + jonObj["PassWord"].ToString());
            Response.Write("<br />");

            //
            string strstwo = "{\"obj\":[{\"ID\":\"2\",\"UserName\":\"Ybvk\",\"PassWord\":\"125\",\"Nickname\":\"放开那段代码\"}]}";
            var c = Newtonsoft.Json.JsonConvert.DeserializeObject<restType>(strstwo);
            Models.Users[] res = c.obj;
            for (int i = 0; i < res.Length; i++)
            {
                Response.Write(res[i].UserName + "------" + res[i].PassWord);
            }
            Response.Write("<br />");
           
            //
            var cv = Newtonsoft.Json.JsonConvert.DeserializeObject<WebForm2>(strstwo);
            Models.Users[] rs = c.obj;
            for (int i = 0; i < rs.Length; i++)
            {
                Response.Write(res[i].UserName + "------" + res[i].PassWord);
            }
            Response.Write("<br />");


            //Json字符串
            var vj = "[{\"ID\":\"2\",\"UserName\":\"Ybvk\",\"PassWord\":\"125\",\"Nickname\":\"放开那段代码\"}]";
            JArray jsonObj = JArray.Parse(vj);
            foreach (JObject jObject in jsonObj)
            {
                Response.Write(jObject["UserName"].ToString());
            }
            Response.Write("<br />");


            //多层嵌套的数组
            string strsfpur = "{\"obj\":[{\"ID\":\"2\",\"UserName\":\"Ybvk\",\"PassWord\":\"125\",\"Nickname\":\"放开那段代码\"}]}";
            JObject jobj= JObject.Parse(strsfpur);
            JArray jar = JArray.Parse(jobj["obj"].ToString());
            JObject j = JObject.Parse(jar[0].ToString());
            Response.Write(j["UserName"].ToString() + j["PassWord"].ToString());
            Response.Write("<br />");
            Response.Write("<br />");


            //Json嵌套Json
            var vj2 = "[{\"obj\":[{\"ID\":\"2\",\"UserName\":\"Ybvk\",\"PassWord\":\"125\",\"Nickname\":\"放开那段代码\"}]}]";
            JArray jsonObj2 = JArray.Parse(vj2);
       
            foreach (JObject jObject2 in jsonObj2)
            {
                JArray jsonObj3 = JArray.Parse(jObject2["obj"].ToString());
                foreach (JObject jObject3 in jsonObj3)
                {
                    Response.Write(jObject3["UserName"].ToString());
                }
            }
        }
    }


    public class restType
    {
        public Models.Users[] obj { get; set; }
    }

 


查看评论

C# Newtonsoft.Json 解析多嵌套json 进行反序列化

[ { "orderNo": "3213123123123", "time": "2016-09-09 12:23:33", "orderSta...
  • kongwei521
  • kongwei521
  • 2016-09-09 17:14:21
  • 9518

C# 生成多层嵌套JSON到数组中(树结构JSON)

DataTable dt = DbHelperSQL.Query(sql).Tables[0]; var changeinfo = string.Empty; ...
  • kongwei521
  • kongwei521
  • 2016-09-13 08:17:43
  • 4130

.net(c#)提取多层嵌套的JSON

Newtonsoft.Json.Net20.dll 下载请访问http://files.cnblogs.com/hualei/Newtonsoft.Json.Net20.rar 在.net 2....
  • gold0523
  • gold0523
  • 2014-07-26 15:25:38
  • 15692

C# json提取多层嵌套到数组

string jsonText = "{'name':'test','phone':'18888888888','image':[{'name':'img1','data':'data1'},{'na...
  • ncqqbesny
  • ncqqbesny
  • 2015-07-23 14:15:52
  • 912

c#解析一个嵌套json的例子

private void okticker_Click(object sender, EventArgs e) { string jsonText = "[{ ...
  • majian
  • majian
  • 2015-12-28 21:10:52
  • 1296

Java中Json字符串直接转换为对象(包括多层List集合 嵌套)

http://blog.csdn.net/catoop/article/details/42744705 使用到的类:net.sf.json.JSONObject   使用JSON时,除了要导...
  • u011001084
  • u011001084
  • 2017-01-05 10:38:06
  • 5383

C# 使用Newtonsoft.Json解析嵌套json

怎么下载Newtonsoft.Json就不说了,直接百度,官网有图文教程;   { "c": 33, "id": 1, "b": [ { ...
  • qq_31776303
  • qq_31776303
  • 2017-10-12 17:01:33
  • 328

JSON的简单使用之提取多层嵌套的JSON(C#)

//将json字符串转换为对象          JObject jObject = (JObject)JsonConvert.DeserializeObject(getString);       ...
  • finish_dream
  • finish_dream
  • 2015-11-19 14:22:58
  • 4105

C#:JSON嵌套对象反系列化,读取对象的值

//引用命名空间 using Newtonsoft.Json; using Newtonsoft.Json.Linq; static void Main(string[] args) { ...
  • hdhai9451
  • hdhai9451
  • 2015-11-25 17:24:34
  • 1699

Gson解析嵌套多层的Json

转载:http://www.cnblogs.com/jxgxy/p/3677256.html 首先先讲一个比较简单点的例子(最简单的我就不讲啦,网上很多),帮助新手理解Gson的使用方法: 比如我们要...
  • snow_Lu
  • snow_Lu
  • 2016-04-01 14:38:05
  • 356
    个人资料
    持之以恒
    等级:
    访问量: 4万+
    积分: 2198
    排名: 2万+
    最新评论