Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

c# - 如何将C#对象转换为.NET中的JSON字符串?(How do I turn a C# object into a JSON string in .NET?)

I have classes like these:

(我有这样的课程:)

class MyDate
{
    int year, month, day;
}

class Lad
{
    string firstName;
    string lastName;
    MyDate dateOfBirth;
}

And I would like to turn a Lad object into a JSON string like this:

(我想将Lad对象变成这样的JSON字符串:)

{
    "firstName":"Markoff",
    "lastName":"Chaney",
    "dateOfBirth":
    {
        "year":"1901",
        "month":"4",
        "day":"30"
    }
}

(without the formatting).

((不带格式)。)

I found this link , but it uses a namespace that's not in .NET 4. I also heard about JSON.NET , but their site seems to be down at the moment, and I'm not keen on using external DLL files.

(我找到了此链接 ,但是它使用的不是.NET 4中的命名空间。我也听说过JSON.NET ,但是目前它们的站点似乎已关闭,并且我不热衷于使用外部DLL文件。)

Are there other options besides manually creating a JSON string writer?

(除了手动创建JSON字符串编写器外,还有其他选择吗?)

  ask by Hui translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since we all love one liners

(既然我们都爱一个班轮)

... this one depends on the Newtonsoft NuGet package, which is popular and better than the default serializer.

(...这取决于Newtonsoft NuGet软件包,该软件包很受欢迎,并且比默认的序列化程序更好。)

Newtonsoft.Json.JsonConvert.SerializeObject(new {foo = "bar"})

Documentation: Serializing and Deserializing JSON

(文档: 序列化和反序列化JSON)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[2] html - How to create even cell spacing within a

2.1m questions

2.1m answers

60 comments

57.0k users

...