反序列化JSON转换成类型的对象牛逼返回0 [英] Deserialize Json into object of type T returns 0

查看:301
本文介绍了反序列化JSON转换成类型的对象牛逼返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想反序列化JSON响应名单,它总是返回0。请看看下面的code和建议修复。

I'm trying to deserialize the JSON response to List, it always returns 0. Please look at the code below and suggest the fix.

JSON数据: -

tripSeats = {"seats":[
  {"available":"true","baseFare":"600","serviceTaxAbsolute":"0"},
  {"available":"true","baseFare":"600","serviceTaxAbsolute":"0"}
]}

我的职业: -

public class Seat
{
   public string available { get; set; }
   public string baseFare { get; set; }
   public string serviceTaxAbsolute { get; set; }
}

public class Seats
{
   public List<Seat> seats { get; set; }
}

现在我试图反序列化JSON数据并将其存储在列表对象下面给出

Now i'm trying to Deserialize the JSON data and store it in List object as given below

JavaScriptSerializer ser = new JavaScriptSerializer();
List<Seats> seats = ser.Deserialize<List<Seats>>(tripSeats.ToString());

它什么都不做,当我调试了code我看到座位有0。

It does nothing, when I debug the code I see seats has 0.

推荐答案

现在的问题是,你正在试图反序列化座椅对象转换为名单&LT;座椅&GT;&GT; 。 你要么需要将其更改为名单,其中,座椅&GT; 或者干脆使用座椅

The problem is you're attempting to deserialize a Seats object into a List<Seats>>. You either need to change it to a List<Seat> or simply use Seats:

JavaScriptSerializer ser = new JavaScriptSerializer();
Seats seats = ser.Deserialize<Seats>(tripSeats);

JavaScriptSerializer ser = new JavaScriptSerializer();
List<Seat> seats = ser.Deserialize<List<Seat>>(tripSeats);

这篇关于反序列化JSON转换成类型的对象牛逼返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆