无法将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
最满意答案
为了使它工作,你必须做这两件事:
以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.
-
Unicode在C ++ 11(Unicode in C++11)[2023-08-06]
上述分析是否正确 让我们来看看。 您不能验证包含有效的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 ...
-
Java unicode字节解析(Java unicode byte parsing)[2023-07-10]
这是"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 ...