Skip to content

Classes: JSONNode.Value

Bunny83 edited this page Aug 29, 2019 · 1 revision

back

public virtual string Value { get; set; }

This property will convert a JSONNode that represents a json primitive value as string. So if you read this property and the node is a

  • JSONString, it will simply return the string it represents.
  • JSONNumber, it will use double.ToString(CultureInfo.InvariantCulture) to convert the number value into a string.
  • JSONBool, it will return bool.ToString()
  • JSONNull, it will return "null"
  • All other classes will simply return an empty string ""

Writing this property will do the reverse and try to convert the string to the actual underlying type. If the node is a

  • JSONString the internal string will simply be set to the given value.
  • JSONNumber it will use double.TryParse() with the invariant culture. If the parsing fails the value will stay unchanged. If the parsing succeeds the value will be changed accordingly.
  • JSONBool bool.TryParse() will be used
  • JSONNull nothing will happen since this is a special constant which will always represent null.
  • All other classes will ignore the usage of the setter.

Remarks

If you need a more specific string conversion of a certain value you might want to read the actual type and do the conversion yourself. For example if you have a JSONNumber and want it to display as an integer with 4 leading zeros you can do:

string num = node.AsInt.ToString("D4");

Note that the Value property will just return the raw data and not json text. If you're looking for how to convert a JSONNode back to json text, see the ToString() methods

Clone this wiki locally