Json.net Property: Single Value or Array of Values

I have a question regarding the JSON.Net library. I usually have an XML string like this:

<Config>
  ....
  <Name>some name</Name>
  ....
</Config>

Then I use the JSON.Net library to convert the string to json string as follows:

Congif: {
   ...
   Name: "some name",
   ...
}

Finally, I map this json string to an instance of the Config class:

Config instance = JsonConvert.DeserializeObject<Config>(json);

If the name property is an array of names in my Config class:

class Config {
   ....
   public string[] Name { get; set; }
   ....
}

I understand that in a json string, an array is defined as follows:

  Name: ["some name"],

Since I get a json string converted from an XMl string, I can have one or more nodes. This causes me problems when only one Name is defined in XMl. I get an exception complaining that it cannot convert a string to a string []. This will be OK if several node names are defined in the XML file.

, - seettings, JSON , Name - , Name?

+3
1

, JSON.Net. jsonString , , - .

, node XML, , XML node node .

, .

+1

Source: https://habr.com/ru/post/1708949/


All Articles