C# json字符串转对像

.Net技术 码拜 9年前 (2016-03-09) 1383次浏览
JSON如下:

{
    "PI": [
        {
            "doc_no": "V160500010",
            "salse": "jizhao",
            "contact_code": "N00000000",
            "customer_contact": "赵先生",
            "customer_code": "C0004280",
            "customer_name": "上海",
            "tel": "18502102883",
            "email": "",
            "address1": "上海新竹路779号",
            "address2": "",
            "city_name": "",
            "pi_date": "2016-05-23",
            "create_by": "peer.ding",
            "sysud": "2016-05-23",
            "bto_addrees1": "",
            "bto_addrees2": "",
            "bto_city_name": "",
            "taxYN": "",
            "sample_ware": "",
            "TOTALAMOUNT": "777",
            "ename": "赵先生",
            "ctel": "18423000033",
            "storefront": "Mo",
            "bto_contactcode": "N00000000",
            "parent_no": "null",
            "cy_type": "",
            "pl_type": ""
        },
        {
            "doc_no": "V160500010",
            "salse": "jimmhao",
            "contact_code": "N00000000",
            "customer_contact": "赵先生",
            "customer_code": "C0000004280",
            "customer_name": "上海",
            "tel": "18502102883",
            "email": "",
            "address1": "上海新竹路779号",
            "address2": "",
            "city_name": "",
            "pi_date": "2016-05-23",
            "create_by": "peer.ding",
            "sysud": "2016-05-23",
            "bto_addrees1": "",
            "bto_addrees2": "",
            "bto_city_name": "",
            "taxYN": "",
            "sample_ware": "",
            "TOTALAMOUNT": "777",
            "ename": "赵先生",
            "ctel": "18423000033",
            "storefront": "M",
            "bto_contactcode": "N00000000",
            "pre_no": "",
            "parent_no": "",
            "cy_type": "",
            "pl_type": ""
        }
    ]
}

C#接收代码如下:
protected int SqlBulkCopyPi()
{
string strTemp = “”, strRe = “”;
if (HttpContext.Current.Request.Form[0] != null)
strTemp = HttpContext.Current.Request.Form[0].ToString();//此处接收的为上面json字符串
//把字符串转成JSON对像
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(strTemp);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(object));
PI_LIST list = o as PI_LIST;//list获取不到一直为空,求高手解答,谢谢
}
其中PI_LIST如下:

public class PI_LIST
{
	public Dictionary<string,PI> pi_ { get; set; }
}
public class PI
{
	public string doc_no { get; set; }
	public string sales { get; set; }
	public string contact_code { get; set; }
	public string customer_contact { get; set; }
	public string customer_code { get; set; }
	public string customer_name { get; set; }
	public string tel { get; set; }
	public string email { get; set; }
	public string address1 { get; set; }
	public string address2 { get; set; }
	public string city_name { get; set; }
	public string pi_date { get; set; }
	public string create_by { get; set; }
	public string sysud { get; set; }
	public string bto_addrees1 { get; set; }
	public string bto_addrees2 { get; set; }
	public string bto_city_name { get; set; }
	public string taxYN { get; set; }
	public string sample_ware { get; set; }
	public string TOTALAMOUNT { get; set; }
	public string ename { get; set; }
	public string ctel { get; set; }
	public string storefront { get; set; }
	public string bto_contactcode { get; set; }
	public string parent_no { get; set; }
	public string cy_type { get; set; }
	//public List<Pi_Order> PL { get; set; }
	public int pl_type { get; set; } 
}
解决方案

40

引用 6 楼 qq_34067000 的回复:

//把字符串转成JSON对像
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(strTemp);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(object));
Root li = o as Root;
用上边生成实体类,li还是null

本人服了你了……..
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(object));
谁教你这样写的?
改成
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(Root ));

Root rt =  serializer.Deserialize<Root>(new JsonTextReader(sr));


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# json字符串转对像
喜欢 (0)
[1034331897@qq.com]
分享 (0)