无法反序列化当前的JSON数组(例如[1,2,3])入式 [英] Cannot deserialize the current JSON array (e.g. [1,2,3]) into type

查看:2076
本文介绍了无法反序列化当前的JSON数组(例如[1,2,3])入式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个类;

public  class MyStok
{
    public int STId { get; set; }
    public int SM { get; set; }
    public string CA { get; set; }
    public string Br { get; set; }
    public string BNo { get; set; }
    public decimal Vat { get; set; }
    public decimal Price { get; set; }
}

我deserilaze这样;

I deserilaze like this;

string sc=  e.ExtraParams["sc"].ToString();
MyStok myobj = JSON.Deserialize<MyStok>(sc);

我的输出似乎喜欢这个(字符串SC)上的提琴手

my output seems to like this ( string sc) on fiddler

[
    {
        "STId": 2,
        "CA": "hbh",
        "Br": "jhnj",
        "SM": 20,
        "Vat": 10,
        "Price": 566,
        "BNo": "1545545"
    }
]

但我得到的错误无法在当前JSON数组(例如[1,2,3])反序列化为类型
什么是错在那些codeS。

but I get the error Cannot deserialize the current JSON array (e.g. [1,2,3]) into type whats wrong in those codes.

感谢您。

推荐答案

它看起来像字符串包含在一个单一的 MyStok 对象的数组。如果从输入的两端去掉括号,你应该能够反序列化数据作为单个对象:

It looks like the string contains an array with a single MyStok object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object:

MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2));

您也可以反序列化数组到 MyStok 对象的列表,并采取在目标指数为零。

You could also deserialize the array into a list of MyStok objects, and take the object at index zero.

var myobjList = JSON.Deserialize<List<MyStok>>(sc);
var myObj = myobjList[0];

这篇关于无法反序列化当前的JSON数组(例如[1,2,3])入式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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