C# json字符串转对像

qq_34067000 2016-05-24 01:26:37
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; }
}

...全文
173 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
songbing774933 2016-05-24
  • 打赏
  • 举报
回复
引用 10 楼 qq_34067000 的回复:
[quote=引用 8 楼 songbing774933 的回复:]
[quote=引用 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));[/quote]


行 96: JsonSerializer serializer = new JsonSerializer();
行 97: StringReader sr = new StringReader(strTemp);
<font color=red>行 98: Root rt = serializer.Deserialize<Root>(new JsonTextReader(sr));
</font>行 99:
<b> 异常详细信息: </b>System.InvalidCastException: 空对象不能转换为值类型。<br><br>
[/quote]

public int pl_type { get; set; }

修改成
public stirng pl_type { get; set; }
或者
public int? pl_type { get; set; }
qq_34067000 2016-05-24
  • 打赏
  • 举报
回复
引用 8 楼 songbing774933 的回复:
[quote=引用 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));[/quote] 行 96: JsonSerializer serializer = new JsonSerializer(); 行 97: StringReader sr = new StringReader(strTemp); <font color=red>行 98: Root rt = serializer.Deserialize<Root>(new JsonTextReader(sr)); </font>行 99: <b> 异常详细信息: </b>System.InvalidCastException: 空对象不能转换为值类型。<br><br>
qq_34067000 2016-05-24
  • 打赏
  • 举报
回复
确认了,接收过来的,和上边的json一样。只需要反序列化对像。但一直为null
songbing774933 2016-05-24
  • 打赏
  • 举报
回复
引用 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));
qq_34067000 2016-05-24
  • 打赏
  • 举报
回复
哪位高人指点指点,怎么老是null
qq_34067000 2016-05-24
  • 打赏
  • 举报
回复
//把字符串转成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
songbing774933 2016-05-24
  • 打赏
  • 举报
回复
public class PIItem
{
/// <summary>
///
/// </summary>
public string doc_no { get; set; }
/// <summary>
///
/// </summary>
public string salse { get; set; }
/// <summary>
///
/// </summary>
public string contact_code { get; set; }
/// <summary>
/// 赵先生
/// </summary>
public string customer_contact { get; set; }
/// <summary>
///
/// </summary>
public string customer_code { get; set; }
/// <summary>
/// 上海
/// </summary>
public string customer_name { get; set; }
/// <summary>
///
/// </summary>
public string tel { get; set; }
/// <summary>
///
/// </summary>
public string email { get; set; }
/// <summary>
/// 上海新竹路779号
/// </summary>
public string address1 { get; set; }
/// <summary>
///
/// </summary>
public string address2 { get; set; }
/// <summary>
///
/// </summary>
public string city_name { get; set; }
/// <summary>
///
/// </summary>
public string pi_date { get; set; }
/// <summary>
///
/// </summary>
public string create_by { get; set; }
/// <summary>
///
/// </summary>
public string sysud { get; set; }
/// <summary>
///
/// </summary>
public string bto_addrees1 { get; set; }
/// <summary>
///
/// </summary>
public string bto_addrees2 { get; set; }
/// <summary>
///
/// </summary>
public string bto_city_name { get; set; }
/// <summary>
///
/// </summary>
public string taxYN { get; set; }
/// <summary>
///
/// </summary>
public string sample_ware { get; set; }
/// <summary>
///
/// </summary>
public string TOTALAMOUNT { get; set; }
/// <summary>
/// 赵先生
/// </summary>
public string ename { get; set; }
/// <summary>
///
/// </summary>
public string ctel { get; set; }
/// <summary>
///
/// </summary>
public string storefront { get; set; }
/// <summary>
///
/// </summary>
public string bto_contactcode { get; set; }
/// <summary>
///
/// </summary>
public string parent_no { get; set; }
/// <summary>
///
/// </summary>
public string cy_type { get; set; }
/// <summary>
///
/// </summary>
public string pl_type { get; set; }
}

public class Root
{
/// <summary>
///
/// </summary>
public List <PIItem > PI { get; set; }
}
songbing774933 2016-05-24
  • 打赏
  • 举报
回复
JSON在线生成实体类

http://www.bejson.com/convert/json2csharp/
qq_34067000 2016-05-24
  • 打赏
  • 举报
回复
嗯嗯,上边二位说的有道理。搞的晕头了,我试试
我叫小菜菜 2016-05-24
  • 打赏
  • 举报
回复
json串是,key-value对象,key是PI,value是一个数组,数组包含两个PI对象。 那怎么转换成Dictionary<string,PI>???? 我觉得,你应该先赋值,然后序列化为字符串,看看字符串是怎样的。 然后反序列化。
  • 打赏
  • 举报
回复
Dictionary<string,PI> 你的PI是个数组,你拿Dictionary来反序列化?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧