class_jsonparseresult.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the JSONParseResult.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_JSONParseResult:
  6. JSONParseResult
  7. ===============
  8. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. **Category:** Core
  10. Brief Description
  11. -----------------
  12. Data class wrapper for decoded JSON.
  13. Properties
  14. ----------
  15. +---------------------------------------+------------------------------------------------------------------+----+
  16. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`error<class_JSONParseResult_property_error>` | |
  17. +---------------------------------------+------------------------------------------------------------------+----+
  18. | :ref:`int<class_int>` | :ref:`error_line<class_JSONParseResult_property_error_line>` | -1 |
  19. +---------------------------------------+------------------------------------------------------------------+----+
  20. | :ref:`String<class_String>` | :ref:`error_string<class_JSONParseResult_property_error_string>` | "" |
  21. +---------------------------------------+------------------------------------------------------------------+----+
  22. | :ref:`Variant<class_Variant>` | :ref:`result<class_JSONParseResult_property_result>` | |
  23. +---------------------------------------+------------------------------------------------------------------+----+
  24. Description
  25. -----------
  26. Returned by :ref:`JSON.parse<class_JSON_method_parse>`, ``JSONParseResult`` contains the decoded JSON or error information if the JSON source wasn't successfully parsed. You can check if the JSON source was successfully parsed with ``if json_result.error == OK``.
  27. Property Descriptions
  28. ---------------------
  29. .. _class_JSONParseResult_property_error:
  30. - :ref:`Error<enum_@GlobalScope_Error>` **error**
  31. +----------+------------------+
  32. | *Setter* | set_error(value) |
  33. +----------+------------------+
  34. | *Getter* | get_error() |
  35. +----------+------------------+
  36. The error type if the JSON source was not successfully parsed. See the :ref:`Error<enum_@GlobalScope_Error>` constants.
  37. ----
  38. .. _class_JSONParseResult_property_error_line:
  39. - :ref:`int<class_int>` **error_line**
  40. +-----------+-----------------------+
  41. | *Default* | -1 |
  42. +-----------+-----------------------+
  43. | *Setter* | set_error_line(value) |
  44. +-----------+-----------------------+
  45. | *Getter* | get_error_line() |
  46. +-----------+-----------------------+
  47. The line number where the error occurred if JSON source was not successfully parsed.
  48. ----
  49. .. _class_JSONParseResult_property_error_string:
  50. - :ref:`String<class_String>` **error_string**
  51. +-----------+-------------------------+
  52. | *Default* | "" |
  53. +-----------+-------------------------+
  54. | *Setter* | set_error_string(value) |
  55. +-----------+-------------------------+
  56. | *Getter* | get_error_string() |
  57. +-----------+-------------------------+
  58. The error message if JSON source was not successfully parsed. See the :ref:`Error<enum_@GlobalScope_Error>` constants.
  59. ----
  60. .. _class_JSONParseResult_property_result:
  61. - :ref:`Variant<class_Variant>` **result**
  62. +----------+-------------------+
  63. | *Setter* | set_result(value) |
  64. +----------+-------------------+
  65. | *Getter* | get_result() |
  66. +----------+-------------------+
  67. A :ref:`Variant<class_Variant>` containing the parsed JSON. Use :ref:`@GDScript.typeof<class_@GDScript_method_typeof>` or the ``is`` keyword to check if it is what you expect. For example, if the JSON source starts with curly braces (``{}``), a :ref:`Dictionary<class_Dictionary>` will be returned. If the JSON source starts with braces (``[]``), an :ref:`Array<class_Array>` will be returned.
  68. **Note:** The JSON specification does not define integer or float types, but only a number type. Therefore, parsing a JSON text will convert all numerical values to float types.
  69. **Note:** JSON objects do not preserve key order like Godot dictionaries, thus, you should not rely on keys being in a certain order if a dictionary is constructed from JSON. In contrast, JSON arrays retain the order of their elements:
  70. ::
  71. var p = JSON.parse('["hello", "world", "!"]')
  72. if typeof(p.result) == TYPE_ARRAY:
  73. print(p.result[0]) # Prints "hello"
  74. else:
  75. print("unexpected results")