C# 如何返回 JSon 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16441880/
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 01:00:55  来源:igfitidea点击:

How to return JSon object

c#jsonjson.net

提问by user1640256

I am using a jQuery plugin that need a JSON object with following structure(I will be retrieving the values from database):

我正在使用一个需要具有以下结构的 JSON 对象的 jQuery 插件(我将从数据库中检索值):

{ results: [
    { id: "1", value: "ABC", info: "ABC" },
    { id: "2", value: "JKL", info: "JKL" },
    { id: "3", value: "XYZ", info: "XYZ" }
] }

Here is my class:

这是我的课:

public class results
{
    int _id;
    string _value;
    string _info;

    public int id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }
    public string value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
        }
    }
    public string info
    {
        get
        {
            return _info;
        }
        set
        {
            _info = value;
        }
    }
}

This is the way I serialize it:

这是我序列化它的方式:

results result = new results();
result.id = 1;
result.value = "ABC";
result.info = "ABC";
string json = JsonConvert.SerializeObject(result);

But this will return only one row. Can you please help me in returning more than one result? How can I get the result in the format specified above?

但这只会返回一行。你能帮我返回一个以上的结果吗?我怎样才能得到上面指定的格式的结果?

回答by ShaneBlake

You only have one row to serialize. Try something like this :

您只有一行要序列化。尝试这样的事情:

List<results> resultRows = new List<results>

resultRows.Add(new results{id = 1, value="ABC", info="ABC"});
resultRows.Add(new results{id = 2, value="XYZ", info="XYZ"});

string json = JavaScriptSerializer.Serialize(new { results = resultRows});
  • Edit to match OP's original json output
  • 编辑以匹配 OP 的原始 json 输出

** Edit 2 : sorry, but I missed that he was using JSON.NET. Using the JavaScriptSerializerthe above code produces this result :

** 编辑 2 :抱歉,我错过了他正在使用 JSON.NET。使用JavaScriptSerializer上面的代码产生这个结果:

{"results":[{"id":1,"value":"ABC","info":"ABC"},{"id":2,"value":"XYZ","info":"XYZ"}]}

回答by Andrew Whitaker

First of all, there's no such thing as a JSON object. What you've got in your question is a JavaScript object literal (see herefor a great discussion on the difference). Here's how you would go about serializing what you've got to JSON though:

首先,没有 JSON 对象这样的东西。您在问题中得到的是 JavaScript 对象文字(有关差异的精彩讨论,请参见此处)。以下是您将如何序列化您所拥有的 JSON 内容的方法:

I would use an anonymous type filled with your resultstype:

我会使用一个用你的results类型填充的匿名类型:

string json = JsonConvert.SerializeObject(new
{
    results = new List<Result>()
    {
        new Result { id = 1, value = "ABC", info = "ABC" },
        new Result { id = 2, value = "JKL", info = "JKL" }
    }
});

Also, note that the generated JSON has result items with ids of type Numberinstead of strings. I doubt this will be a problem, but it would be easy enough to change the type of idto stringin the C#.

另请注意,生成的 JSON 具有id类型Number为s而不是字符串的结果项。我怀疑这将是一个问题,但它是很容易的改变的类型id,以string在C#。

I'd also tweak your resultstype and get rid of the backing fields:

我还会调整您的results类型并摆脱支持字段:

public class Result
{
    public int id { get ;set; }
    public string value { get; set; }
    public string info { get; set; }
}

Furthermore, classes conventionally are PascalCasedand not camelCased.

此外,类通常是PascalCased和不是camelCased

Here's the generated JSON from the code above:

这是从上面的代码生成的 JSON:

{
  "results": [
    {
      "id": 1,
      "value": "ABC",
      "info": "ABC"
    },
    {
      "id": 2,
      "value": "JKL",
      "info": "JKL"
    }
  ]
}