Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Check if JSON is valid using JSONObject

Discussion in 'Scripting' started by gdennis, Jan 8, 2015.

  1. gdennis

    gdennis

    Joined:
    Oct 22, 2014
    Posts:
    17
    I am using JSONObject (http://wiki.unity3d.com/index.php?title=JSONObject) to parse a JSON file. I need to find out if the JSON is valid or not. I am using C#.

    The JSON could look like this:


    Code (JSON):
    1. {
    2.         "_entries": [
    3.             "g0"
    4.         ],
    5.         "_flow": {
    6.             "g1": {
    7.                 "_expr": [
    8.                     {
    9.                         "word_": "player",
    10.                         "_next": [
    11.                             {
    12.                                 "word_": "gives",
    13.                                 "_next": [
    14.                                     {
    15.                                         "word_": "Shovel"
    16.                                     }
    17.                                 ]
    18.                             },
    19.                             {
    20.                                 "word_": "to",
    21.                                 "_next": [
    22.                                     {
    23.                                         "word_": "Elisabeth"
    24.                                     }
    25.                                 ]
    26.                             }
    27.                         ]
    28.                     }
    29.                 ],
    30.                 "_interpr": {
    31.                     "_subject": "player",
    32.                     "_predicate": {
    33.                         "_verb": "gives to",
    34.                         "_direct": "Shovel",
    35.                         "_indirect": "Elisabeth"
    36.                     }
    37.                 }
    38.             },
    39.             "g0": {
    40.                 "_next": "g1",
    41.                 "_expr": [
    42.                     {
    43.                         "word_": "Mary",
    44.                         "_next": [
    45.                             {
    46.                                 "word_": "gives",
    47.                                 "_next": [
    48.                                     {
    49.                                         "word_": "Shovel"
    50.                                     }
    51.                                 ]
    52.                             },
    53.                             {
    54.                                 "word_": "to",
    55.                                 "_next": [
    56.                                     {
    57.                                         "word_": "player"
    58.                                     }
    59.                                 ]
    60.                             }
    61.                         ]
    62.                     }
    63.                 ],
    64.                 "_interpr": {
    65.                     "_subject": "Mary",
    66.                     "_predicate": {
    67.                         "_verb": "gives to",
    68.                         "_direct": "Shovel",
    69.                         "_indirect": "player"
    70.                     }
    71.                 }
    72.             }
    73.         }
    74.     }

    I want to check on a couple of things :
    - the format is valid
    - it has a "_entries" field
    - it has a "_flow" field
    - every "g(x)" element has an "_interpr" field


    How do I do this?


    Thanks in advance to anyone who can help me!
     
  2. gurayg

    gurayg

    Joined:
    Nov 28, 2013
    Posts:
    269
    Not sure if answers your question but;
    "JSON Object" asset from the Asset Store seems to do such a job.
    https://www.assetstore.unity3d.com/en/#!/content/710

    I haven't used this but was looking for a similar thing and came across this.
    From the screenshot it seems like it has a JSON checker functionality.
    Hope that helps.
     
  3. gdennis

    gdennis

    Joined:
    Oct 22, 2014
    Posts:
    17
    I used this package already, however I just copied it from the unity community website and not from the asset. The json checking class was not in there, but looking at it, it helped me a bit further! Thankyou :)