Search Unity

Question How to parse this JSON file?

Discussion in 'Scripting' started by FeastSC2, Nov 13, 2022.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    How can I parse this JSON using unity's JsonUtility.FromJson?

    I'm not sure what kind of structure I need to create considering that there is more than 1 definition.

    This the Json. It contains the various definition of the word "bottle".

    Code (CSharp):
    1. [
    2.   {
    3.     "partOfSpeech": "noun",
    4.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    5.     "sourceDictionary": "wiktionary",
    6.     "text": "A container, typically made of glass and having a tapered neck, used for holding liquids.",
    7.     "word": "bottle",
    8.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    9.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    10.     "citations": [],
    11.     "exampleUses": [],
    12.     "labels": [],
    13.     "notes": [],
    14.     "relatedWords": [],
    15.     "textProns": []
    16.   },
    17.   {
    18.     "partOfSpeech": "noun",
    19.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    20.     "sourceDictionary": "wiktionary",
    21.     "text": "The contents of such a container.",
    22.     "word": "bottle",
    23.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    24.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    25.     "citations": [],
    26.     "exampleUses": [],
    27.     "labels": [],
    28.     "notes": [],
    29.     "relatedWords": [],
    30.     "textProns": []
    31.   },
    32.   {
    33.     "partOfSpeech": "noun",
    34.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    35.     "sourceDictionary": "wiktionary",
    36.     "text": "A container with a rubber nipple used for giving liquids to infants",
    37.     "word": "bottle",
    38.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    39.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    40.     "citations": [],
    41.     "exampleUses": [],
    42.     "labels": [],
    43.     "notes": [],
    44.     "relatedWords": [],
    45.     "textProns": []
    46.   },
    47.   {
    48.     "partOfSpeech": "noun",
    49.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    50.     "sourceDictionary": "wiktionary",
    51.     "text": "<xref>Nerve</xref>, <xref>courage</xref>.",
    52.     "labels": [
    53.       {
    54.         "text": "UK",
    55.         "type": "region"
    56.       },
    57.       {
    58.         "text": "informal",
    59.         "type": "register"
    60.       }
    61.     ],
    62.     "word": "bottle",
    63.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    64.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    65.     "citations": [],
    66.     "exampleUses": [],
    67.     "notes": [],
    68.     "relatedWords": [],
    69.     "textProns": []
    70.   },
    71.   {
    72.     "partOfSpeech": "noun",
    73.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    74.     "sourceDictionary": "wiktionary",
    75.     "text": "With one's <xref>hair</xref> color produced by <xref>dyeing</xref>.",
    76.     "labels": [
    77.       {
    78.         "text": "attributive",
    79.         "type": "usage"
    80.       }
    81.     ],
    82.     "word": "bottle",
    83.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    84.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    85.     "citations": [],
    86.     "exampleUses": [],
    87.     "notes": [],
    88.     "relatedWords": [],
    89.     "textProns": []
    90.   },
    91.   {
    92.     "partOfSpeech": "noun",
    93.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    94.     "sourceDictionary": "wiktionary",
    95.     "text": "A bundle, especially of hay; something tied in a bundle.",
    96.     "labels": [
    97.       {
    98.         "text": "obsolete",
    99.         "type": "chronological"
    100.       }
    101.     ],
    102.     "word": "bottle",
    103.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    104.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    105.     "citations": [],
    106.     "exampleUses": [],
    107.     "notes": [],
    108.     "relatedWords": [],
    109.     "textProns": []
    110.   },
    111.   {
    112.     "partOfSpeech": "verb",
    113.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    114.     "sourceDictionary": "wiktionary",
    115.     "text": "To seal (a liquid) into a bottle for later consumption.",
    116.     "labels": [
    117.       {
    118.         "text": "transitive",
    119.         "type": "grammar"
    120.       }
    121.     ],
    122.     "word": "bottle",
    123.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    124.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    125.     "citations": [],
    126.     "exampleUses": [],
    127.     "notes": [],
    128.     "relatedWords": [],
    129.     "textProns": []
    130.   },
    131.   {
    132.     "partOfSpeech": "verb",
    133.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    134.     "sourceDictionary": "wiktionary",
    135.     "text": "To feed (an infant) <xref>baby formula</xref>.",
    136.     "labels": [
    137.       {
    138.         "text": "transitive",
    139.         "type": "grammar"
    140.       },
    141.       {
    142.         "text": "UK",
    143.         "type": "region"
    144.       }
    145.     ],
    146.     "word": "bottle",
    147.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    148.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    149.     "citations": [],
    150.     "exampleUses": [],
    151.     "notes": [],
    152.     "relatedWords": [],
    153.     "textProns": []
    154.   },
    155.   {
    156.     "partOfSpeech": "verb",
    157.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    158.     "sourceDictionary": "wiktionary",
    159.     "text": "To refrain from doing (something) at the last moment because of a sudden loss of courage.",
    160.     "labels": [
    161.       {
    162.         "text": "UK",
    163.         "type": "region"
    164.       },
    165.       {
    166.         "text": "slang",
    167.         "type": "register"
    168.       }
    169.     ],
    170.     "word": "bottle",
    171.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    172.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    173.     "citations": [],
    174.     "exampleUses": [],
    175.     "notes": [],
    176.     "relatedWords": [],
    177.     "textProns": []
    178.   },
    179.   {
    180.     "partOfSpeech": "verb",
    181.     "attributionText": "from Wiktionary, Creative Commons Attribution/Share-Alike License.",
    182.     "sourceDictionary": "wiktionary",
    183.     "text": "To strike (someone) with a bottle.",
    184.     "labels": [
    185.       {
    186.         "text": "UK",
    187.         "type": "region"
    188.       },
    189.       {
    190.         "text": "slang",
    191.         "type": "register"
    192.       }
    193.     ],
    194.     "word": "bottle",
    195.     "attributionUrl": "http://creativecommons.org/licenses/by-sa/3.0/",
    196.     "wordnikUrl": "https://www.wordnik.com/words/bottle",
    197.     "citations": [],
    198.     "exampleUses": [],
    199.     "notes": [],
    200.     "relatedWords": [],
    201.     "textProns": []
    202.   }
    203. ]
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    You cannot.

    Problems with Unity "tiny lite" built-in JSON:

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io
     
    FeastSC2 and Olipool like this.
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Looks like a great solution but unfortunately it seems that this asset is not compatible with the newer versions of Unity.
    There are some assembly exceptions on Unity v2021.3
     
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Last edited: Nov 14, 2022
    FeastSC2 and Bunny83 like this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    Yes and no :) The structure itself should be supported by Unity's JsonUtility. However the JsonUtility does always expect an object as root element. So you can easily wrap the data in an object. The JsonUtility just has the same limitations as Unity's serialization system and the data shown here should work when wrapped in an object.

    Note: We can't tell you what you should use for all those empty arrays as it's not clear what their content may be. For example, the "labels" array contains objects with a "text" and "type" field. We don't know what may be returned in those other arrays that are always empty in your example. You probably should consult the documentation of the API you're using to know how the structure looks like. A single example most likely does not cover everything.

    Though, maybe you don't need everything. You can create objects which may only partially map the data that you're interested in. So any additional data is simply ignored.

    So it highly depends on what you actually need. If you just want to access certain fields, you could use my SimplyJSON parser which does not require that you create any custom classes. It just parses the structure into JSONObjects, JSONArrays and other classes for each type json supports. It just simplifies the way you can handle the data.
     
    FeastSC2 likes this.