C# 如何在代码中编写 JSON 字符串值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13489139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:44:41  来源:igfitidea点击:

How to write JSON string value in code?

c#jsonstringsyntax

提问by user1811801

I want to store the following string in a String variable

我想将以下字符串存储在 String 变量中

{"Id":"123","DateOfRegistration":"2012-10-21T00:00:00+05:30","Status":0}

{"Id":"123","DateOfRegistration":"2012-10-21T00:00:00+05:30","Status":0}

This is the code I use ..

这是我使用的代码..

String str="{"Id":"123","DateOfRegistration":"2012-10-21T00:00:00+05:30","Status":0}";

.. but it's showing error ..

.. 但它显示错误..

采纳答案by Freak

You have to do this

你必须这样做

String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}";


Please see this for reference
Also from msdn :)


参阅此以供参考
来自 msdn :)

Short Notation  UTF-16 character    Description
\'  \u0027  allow to enter a ' in a character literal, e.g. '\''
\"  \u0022  allow to enter a " in a string literal, e.g. "this is the double quote (\") character"
\  \u005c  allow to enter a \ character in a character or string literal, e.g. '\' or "this is the backslash (\) character"
String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}";
\u0000 allow to enter the character with code 0 \a \u0007 alarm (usually the HW beep) \b \u0008 back-space \f \u000c form-feed (next page) \n \u000a line-feed (next line) \r \u000d carriage-return (move to the beginning of the line) \t \u0009 (horizontal-) tab \v \u000b vertical-tab

回答by Basti M

You have to escape the quotes within the string like this:

您必须像这样转义字符串中的引号:

String str="{\"Id\":\"123\",\"DateOfRegistration\":\"2012-10-21T00:00:00+05:30\",\"Status\":0}";

回答by Brian

you need to escape the inner quotes like so:

你需要像这样转义内部引号:

string str = "{'Id':'123','DateOfRegistration':'2012 - 10 - 21T00: 00:00 + 05:30','Status':0}".Replace("'", "\"");

回答by Mohammad Nikravan

I prefer this, just make sure you don't have single quote in the string

我更喜欢这个,只要确保字符串中没有单引号

dynamic contact = new ExpandoObject();

contact.Name = “Patrick Hines”;

contact.Phone = “206-555-0144”;

contact.Address = new ExpandoObject();

contact.Address.Street = “123 Main St”;

contact.Address.City = “Mercer Island”;

contact.Address.State = “WA”;

contact.Address.Postal = “68402”;

//Serialize to get Json string using NewtonSoft.JSON
string Json = JsonConvert.SerializeObject(contact);

回答by sudhAnsu63

There is an alternate way to write these complex JSON using Expando object or XElement and then serialize.

有一种替代方法可以使用 Expando 对象或 XElement 编写这些复杂的 JSON,然后进行序列化。

https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/

https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/

##代码##