首页 \ 问答 \ 无法将JSON数组反序列化为C#列表(Not able to deserialize JSON array into C# list)

无法将JSON数组反序列化为C#列表(Not able to deserialize JSON array into C# list)

我有一个json数组如下

{
"odata.metadata": "https://testapp.mycomp.com/$metadata#UnitDetail",
"value": [
    {
        "Id": 1,
        "UnitId": 238905,
        "Active": false,
        "Name": "Rakesh",
        "ContactNumber": "0070002934"           
    },
    {
        "Id": 2,
        "UnitId": 238906,
        "Active": true,
        "Name": "Rahul",
        "ContactNumber": "123444003"
    },
    {
        "Id": 3,
        "UnitId": 238907,
        "Active": true,
        "Name": "Rohit",
        "ContactNumber": "1227032932"
    }
]

}

我试图将其反序列化为c#list,如下所示

 var data= JsonConvert.DeserializeObject<UnitDetail[]>(json);

我的c#课程如下

  public class UnitDetail
{
    public int Id { get; set; }
    public int UnitId { get; set; }
    public bool Active { get; set; }
    public string Name { get; set; }
    public string ContactNumber { get; set; }

}

但是这段代码没有反序列化。

我也尝试过如下代码但似乎没有工作

  JavaScriptSerializer js = new JavaScriptSerializer();
  UnitDetail[] serializedData= js.Deserialize<UnitDetail[]>(json);

还有如下

List<UnitDetail> serializedData= js.Deserialize<List<UnitDetail>>(json);

我真的不确定它为什么不起作用。 任何帮助都会非常有用。

谢谢


I have a json array as below

{
"odata.metadata": "https://testapp.mycomp.com/$metadata#UnitDetail",
"value": [
    {
        "Id": 1,
        "UnitId": 238905,
        "Active": false,
        "Name": "Rakesh",
        "ContactNumber": "0070002934"           
    },
    {
        "Id": 2,
        "UnitId": 238906,
        "Active": true,
        "Name": "Rahul",
        "ContactNumber": "123444003"
    },
    {
        "Id": 3,
        "UnitId": 238907,
        "Active": true,
        "Name": "Rohit",
        "ContactNumber": "1227032932"
    }
]

}

I am trying to deserialize it into c# list as below

 var data= JsonConvert.DeserializeObject<UnitDetail[]>(json);

My c# class is a below

  public class UnitDetail
{
    public int Id { get; set; }
    public int UnitId { get; set; }
    public bool Active { get; set; }
    public string Name { get; set; }
    public string ContactNumber { get; set; }

}

But this code is not deserializing it.

I have also tried codes as below but none seems working

  JavaScriptSerializer js = new JavaScriptSerializer();
  UnitDetail[] serializedData= js.Deserialize<UnitDetail[]>(json);

and also like below

List<UnitDetail> serializedData= js.Deserialize<List<UnitDetail>>(json);

I am really not sure why it is not working. Any help would really be appriciable.

Thanks


原文:https://stackoverflow.com/questions/32501351
更新时间:2023-09-12 17:09

最满意答案

为了使它工作,你必须做这两件事:

  • 以Unicode格式(UTF-8)保存源文件。 如何执行此操作依赖于IDE /文本编辑器。

  • 通过指定UTF-8字符集来编译文件。 像这样:

javac -encoding utf-8 MyFile.java


In order for it to work you must do these 2 things:

  • Save the source file in Unicode format (UTF-8). How to do this is IDE/Text Editor dependent.

  • Compile the file by specifying the UTF-8 charset. Like this:

javac -encoding utf-8 MyFile.java

相关问答

更多
  • 为了使它工作,你必须做这两件事: 以Unicode格式(UTF-8)保存源文件。 如何执行此操作依赖于IDE /文本编辑器。 通过指定UTF-8字符集来编译文件。 像这样: javac -encoding utf-8 MyFile.java In order for it to work you must do these 2 things: Save the source file in Unicode format (UTF-8). How to do this is IDE/Text Editor d ...
  • 是的,文件已过时。 脚本组件在其早期阶段确实经历了一系列更改(如果您使用的是早期绑定,其中一些更改会发生变化),但至少WK2000 SP4和XP SP2已经非常稳定。 只要注意unicode的含义即可。 有时unicode这个词更广泛地使用,可以涵盖unicode的任何编码。 FSO不读取例如unicode的UTF8编码。 为此,您需要回退ADODB.Stream。 Yes that documentation is out of date. The scripting component did go t ...
  • .INI文件很旧。 它们是在引入Unicode之前的几十年。 它们是简单的ASCII文件。 大量的应用程序(包括我的)使用简单的ASCII Api(如GetPrivateProfileString )与它们一起工作。 如果您的应用程序使用Unicode默认值,则可以显式编写GetPrivateProfileStringA 。 这将迫使它的所有参数都是简单的字符串。 .INI files are very old stuff. They were existing decades before the Uni ...
  • my_string.replaceAll("\\p{C}", "?"); 详细了解Unicode正则表达式 。 java.util.regexPattern / String.replaceAll支持它们。 my_string.replaceAll("\\p{C}", "?"); See more about Unicode regex. java.util.regexPattern/String.replaceAll supports them.
  • 上述分析是否正确 让我们来看看。 您不能验证包含有效的UTF-8的字节数组 不正确。 std::codecvt_utf8::length(start, end, max_lenght)返回数组中有效字节数。 你找不到长度 部分正确。 可以转换为char32_t,找出结果的长度。 没有简单的方法找出长度,而不做实际的转换(但见下文)。 我必须说,需要统计字符(在任何意义上)都是很少出现的。 您不能以除逐字节之外的任何方式迭代std :: string 不正确。 std::codecvt_u ...
  • FileReader显然假设编码与ASCII兼容。 (可能期望UTF-8或任何旧的ASCII扩展)。 此外,它不是“Unicode文件” - 它是“UTF-16编码文件”。 您必须使用StreamReader并自己指定编码: BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pathname), "UTF-16LE")); 你也应该真正阅读那篇文章 - 在我看来,你对字符集和编码有很多误解。 T ...
  • 这是"UTF-16LE" 。 0x00 0x00实际上是编码UTF-16中的NUL字符,所以这就是你将得到的。 该编码可以编码大约一百万个不同的字符,每个字符使用2或4个字节。 前256个字符用第二个字节0x00编码,如果文本只包含那些可能被视为无用的字符,但它是其余字符所必需的。 例如,欧元货币符号€将显示为0xAC 0x20 。 That's "UTF-16LE". 0x00 0x00 actually encodes the NUL character in UTF-16 so that's what ...
  • 这不是字符串连接的问题。 这是显示字体的问题。 它只是不支持角色。 如果我在我的机器上尝试它,标准显示字体支持完全unicode,结果如下: 您应该尝试使用具有支持的字体,而不是标准字体。 This is not a problem of a string concatenation. It's a problem of the display font. It just doesn't support the character. If I try it on my machine where the s ...
  • 对于所有的ASCII字符,UTF-8使用1个字节,其与标准ASCII编码具有相同的代码值,对于其他字符最多4个字节。 每个字节的高位保留为控制位。 对于使用多于1个字节的代码点,控制位被设置。 因此UTF-8文件中不应该有0个字符。 检查维基百科的UTF-8 UTF-8 uses 1 byte for all ASCII characters, which have the same code values as in the standard ASCII encoding, and up to 4 byt ...
  • 如果在源文件的开头没有BOM 1签名,VS将不会检测编码。 如果没有BOM,它将假设本地化的ANSI编码。 BOM签名标识所使用的UTF8 / 16/32编码。 因此,如果您将某些内容保存为UTF-8(VS将添加BOM)并删除前3个字节(EF BB BF),则该文件将在美国Windows上解释为CP1252,而在中文Windows上将被解释为GB2312等。 您使用的是中文Windows,因此要么保存为GB2312(无BOM)或UTF8(带BOM),以便正确解码源代码。 1 https://en.wikip ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)