• C#解析Json数组对象(利用AjaxPro实现)


    1. 前端Json数组定义

      var jsonArr=new Array();
      var json1={"Name":"zhangsan","Age":15,"Sex":"male"};
      var json2={"Name":"lisi","Age":10,"Sex":"female"};
      jsonArr.push(json1);
      jsonArr.push(json2);

    2. 后端:C# 定义与传入的Json对象类似的类,用来接收Json对象
        这里要注意:传入的Json必须和后台自定义的类的属性值一样。

        public class People
        {
            public string Name;
            public int Age;
            public string Sex;
        }

    3. 采用AjaxPro方法传入后端,然后利用AjaxPro.JavaScriptDeserializer类的静态方法DeserializeFromJson(string json,Type type)
       

            [AjaxPro.AjaxMethod]
            public void DeserializeJson(object jsonObj)
            {
                List<People> lstResult = new List<People>();
                lstResult = (List<People>)JavaScriptDeserializer.DeserializeFromJson(jsonObj.ToString(), typeof(List<People>));
            }

        或者先转换为AjaxPro.JavaScriptArray对象,然后遍历每项,调用AjaxPro.JavaScriptDeserializer类的静态方法Deserialize(IJavaScriptObject object,Type type)

    AjaxPro.JavaScriptArray array = (AjaxPro.JavaScriptArray)JavaScriptDeserializer.DeserializeFromJson(jsonObj.ToString(), typeof(AjaxPro.JavaScriptArray));
                for (int i = 0; i < array.Count; i++)
                {
                    AjaxPro.JavaScriptObject item = (AjaxPro.JavaScriptObject)array[i];
                    People people=(People)AjaxPro.JavaScriptDeserializer.Deserialize(item, typeof(People));
                    lstResult.Add(people);
                }
  • 相关阅读:
    Mysql Dump
    Amazon的Dynamo中用到的几种技术
    一致性哈希
    eclipse下提交job时报错mapred.JobClient: No job jar file set. User classes may not be found.
    eclipse中java heap space问题解决方法
    hadoop的启动步骤
    java根据经纬度计算距离
    eclipse创建Enterprise Application project没有web.xml
    cygwin+hadoop安装步骤和问题
    java程序员修炼
  • 原文地址:https://www.cnblogs.com/stevemin/p/3540715.html
Copyright © 2020-2023  润新知