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

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 lstResult = new List();
            lstResult = (List)JavaScriptDeserializer.DeserializeFromJson(jsonObj.ToString(), typeof(List));
        }

 

  或者先转换为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);
            }

 

你可能感兴趣的:(前端,C#)