简体   繁体   English

在 C# 中从 Object 转换 Json 时获取属性名称

[英]Getting property name while converting Json from Object in C#

I have a class like below:我有一个 class 如下所示:

public class PCBulkRequest 
{ 
    public List<PCRequest> pcRequest { get; set; } 
}

When I'm trying to convert this class to a JSON string I'm getting JSON with the property name included, which I don't want.当我尝试将此 class 转换为 JSON 字符串时,我得到了包含属性名称的 JSON ,这是我不想要的。

string json = JsonConvert.SerializeObject(pcRequest, Formatting.Indented);

Result:结果:

{
  "pcRequest": [
    {
      "name": "John",
      "surname": "Elton"
    },
    {
      "name": "John",
      "surname": "Elton"
    }
  ]
}

Expected result:预期结果:

[
  {
    "name": "John",
    "surname": "Elton"
  },
  {
    "name": "John",
    "surname": "Elton"
  }
]

Just serialize the list directly:直接序列化列表即可:

string json = JsonConvert.SerializeObject(pcRequest.pcRequest, Formatting.Indented);

As an aside, your naming is slightly confusing at the moment - it sounds like you have a variable called pcRequest that's of type PCBulkRequest , and your pcRequest property sounds like it should be a single request when it's actually a list.顺便说一句,您的命名目前有点令人困惑-听起来您有一个名为pcRequest的变量,其类型为PCBulkRequest ,并且您的pcRequest属性听起来应该是一个请求,而实际上它是一个列表。 I'd have names something like this:我会有这样的名字:

public class PCBulkRequest
{
    public List<PCRequest> Requests { get; set; }
}

...

var bulkRequest = GetBulkRequest(); // Wherever this comes from
string json = JsonConvert.SerializeObject(bulkRequest.Requests, Formatting.Indented);

We can't see how you defined it, but based on the result you're getting it seems that pcRequest in your code must be an instance of the PCBulkRequest class.我们看不到您是如何定义它的,但根据您得到的结果,您的代码中的pcRequest似乎必须是PCBulkRequest class 的实例。

Therefore Json.NET will serialise the entire object, as you instructed it to.因此 Json.NET 将按照您的指示序列化整个 object。 If you only want to serialise the list, then it's quite simple: you need to supply the list from within that object as the item to be serialised.如果您只想序列化列表,那么它非常简单:您需要从 object 中提供列表作为要序列化的项目。

string json = JsonConvert.SerializeObject(pcRequest.pcRequest, Formatting.Indented);

The Json.NET library can only serialise what you tell it to, it can't guess which sub-section you actually wanted serialising! Json.NET 库只能序列化您告诉它的内容,它无法猜测您实际想要序列化的子部分!


NB I would suggest maybe naming your variables more clearly so that you don't re-use the name of a property within the class as a variable name for an instance of that class.注意,我建议您更清楚地命名您的变量,这样您就不会重复使用 class 中的属性名称作为该 class 实例的变量名称。 It can get a bit confusing.它可能会有点混乱。 And the property name pcRequest is singular, so it sounds like it would hold a single request, when in fact it's a list - that can also get confusing.并且属性名称pcRequest是单数的,所以听起来它会包含一个请求,而实际上它是一个列表 - 这也会让人感到困惑。 Your code will be much more maintainable and understandable (both for you and for others) if you take the time to give your variables meaningful, clear names.如果您花时间为变量赋予有意义、清晰的名称,您的代码将更易于维护和理解(对您和其他人而言)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM