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

时间:2012-11-21 08:29:25

标签: c# json string syntax

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

  

{ “ID”: “123”, “DateOfRegistration”: “2012-10-21T00:00:00 + 05:30”, “状态”:0}

这是我使用的代码..

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

..但它显示错误..

8 个答案:

答案 0 :(得分:29)

你必须这样做

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


see this for reference
另外from 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"
\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

答案 1 :(得分:7)

我更喜欢这个,只要确保你在字符串

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

答案 2 :(得分:5)

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

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

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);

答案 3 :(得分:4)

您必须转义字符串中的引号,如下所示:

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

答案 4 :(得分:2)

你需要像这样逃避内部引号:

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

答案 5 :(得分:2)

在@ sudhAnsu63上进行微调,这是一个单线

使用 NET Core

string str = JsonSerializer.Serialize(
  new {    
    Id = 2,
    DateOfRegistration = "2012-10-21T00:00:00+05:30",
    Status = 0
  }
);

使用 Json.Net

string str = JsonConvert.SerializeObject(
  new {    
    Id = 2,
    DateOfRegistration = "2012-10-21T00:00:00+05:30",
    Status = 0
  }
);

不需要实例化dynamic ExpandoObject

答案 6 :(得分:0)

对于开箱即用的解决方案,我将 JSON 编码为 base64,以便它可以在一行中作为字符串值导入。

这会保留行格式,而无需您手动编写动态对象或转义字符。格式与从文本文件中读取 JSON 的格式相同:

var base64 = "eyJJZCI6IjEyMyIsIkRhdGVPZlJlZ2lzdHJhdGlvbiI6IjIwMTItMTAtMjFUMDA6MDA6MDArMDU6MzAiLCJTdGF0dXMiOjB9";
byte[] data = Convert.FromBase64String(base64);
string json = Encoding.UTF8.GetString(data);

//using the JSON text
var result = JsonConvert.DeserializeObject<object>(json);

答案 7 :(得分:0)

使用逐字字符串文字 (@"..."),您可以通过将双引号与双引号对交换来编写内联多行 json - "" 而不是 "。示例:

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