Search Unity

Json File Formats

Discussion in 'Scripting' started by SF11Dude, Nov 25, 2018.

  1. SF11Dude

    SF11Dude

    Joined:
    Nov 8, 2018
    Posts:
    13
    I have been to several sites including jsonlint and tried just about every format and tip including those here and I cannot figure out what is happening, if it is a data or logic issue.

    If my data file (Art.json) only has the first line, it works fine; once I add a comma and the second line, it pukes and dies, I tried array, string, etc...qnumber is an integer as is ICAnswer but it works fine with/without quotes as long as there is only 1 line of data.

    Thoughts?
     
    Last edited: Nov 26, 2018
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    As @Antypodish said it's good to use a JSON validator to find errors. Your issue is that you have an array but no array parent. If you did something like this it would work:
    Code (CSharp):
    1. {
    2.   "questions": [
    3.     {
    4.       "qnumber": "0",
    5.       "ICAnswer": "5"
    6.     },
    7.     {
    8.       "qnumber": "0",
    9.       "ICAnswer": "5"
    10.     }
    11.   ]
    12. }
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Can you show us TrivialCard class?
    Also, put some Debug.Log at intermediate steps, form file name, to json parsing, and parsed fields, etc. Test at which step your script fails.

    Ensure for example, JsonString is loaded as expected.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Ah sorry, just not get use to screenshots like that. Is not very friendly to read.
    Why you just don't paste script and use Tag Code to display code nicely?
    See Using code tags properly

    So lets start from beggining.
    First debug log after jsonString. Lets see, what you read from the file.

    Then out Debug break point, just after FromJson line, and investigate manually content of Bonzo.

    When you check, describe what you have read. Then we will pick from there.
     
  6. You're trying to create one object out of an entire array... Don't you have errors in the console?
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Providing you are reading right file, you should go back to @tonemcbride post above and format your json content accordingly.

    You can create new class in start
    Code (CSharp):
    1. TrivaCard tc = new TriviaCard () ;
    Then you put some basics data there, as template.
    And then use ToJson, to generate dummy json data.
    Use it to fill with correct data.
     
  8. I'm lost, I can't see what you're doing in your code and the small pieces of screenshots don't help it at all. Would you please submit your code here in code tags? Maybe we can spot what you're doing wrong if we actually see your code in text format with proper highlight.
     
    Antypodish likes this.
  9. unity_bwoR1uRsjfB-8A

    unity_bwoR1uRsjfB-8A

    Joined:
    Dec 11, 2018
    Posts:
    1
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    It's null because you aren't casting it correctly. Your triviaCard is a single instance in your class, but your json string shows it's a collection.

    http://json2csharp.com/

    Take your json string and put it into that site and it will show you your class structure.

    I also can't remember what the limitations are for Unity's json serializer is and collections as the base level, as I tend to use json.net personally.
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    RootObject(you can change that name).

    So you need to deserialize into that class. Can't give you an example currently. Also for Unity's json you have to use fields with the serializable attribute like normal. Properties can be used with json.net, but not Unity's json.

    If you weren't using screenshots, I could copy and paste and fix things. but you should turn all the properties into fields, then maybe change RootObject into JsonWrapper (since you're using that already).

    Make sure both classes include the [System.Serializable], then it should work with your current code. Just make sure to remove your other JsonWrapper class.
     
  12. SF11Dude

    SF11Dude

    Joined:
    Nov 8, 2018
    Posts:
    13
    Forgot to mention I did check the properties on the objects and they are all normal.
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    From the look of things...you didn't follow what I told you to do...

    TriviaCard.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class TriviaCard
    7. {
    8.     public string qNumber;
    9.     public string ICAnswer;
    10.     public string question1;
    11.     public string answer1;
    12.     public string answer2;
    13.     public string answer3;
    14.     public string answer4;
    15.     public string answer5;
    16.     public string answer6;
    17. }
    18.  
    19. [System.Serializable]
    20. public class JsonWrapper
    21. {
    22.     public List<TriviaCard> triviaCard;
    23. }
    Other Script (assign your string to jsonString variable
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TriviaTest : MonoBehaviour {
    6.  
    7.     string jsonString;
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.         //Assign json string to the jsonString variable
    12.         JsonWrapper jw = JsonUtility.FromJson<JsonWrapper>(jsonString);
    13.         Debug.Log(jw.triviaCard[0].question1);
    14.     }
    15.      
    16.     }
    17. }
     
  14. SF11Dude

    SF11Dude

    Joined:
    Nov 8, 2018
    Posts:
    13
    "I did try to change this like you suggested but I am obviously not clear on the references..."; meant that I tried to change it and it didn't work so I put it back and just provided the code...I thought that was the best thing to do so it wouldn't get worse...

    I deleted my other JsonWrapper class, made the changes you provided (thank you) but still getting the message...

    NullReferenceException: Object reference not set to an instance of an object
    JsonData.Start () (at Assets/Scripts/JsonData.cs:31)


    Debug.Log(jw.triviaCard[0].question1);
    this is the problem statement...
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You'd only get null if the string wasn't deserialized into the variable. I did a test string and it worked perfectly fine. Make sure that jsonString variable is being assigned your string. Also note I did this as a local variable, so it only last within the Start method. If you need it to be accessible outside of Start, make sure you are assigning it to a class variable.

    You can post your code again with the modifications made to it and I'll look at it to see if anything stands out.
     
  16. SF11Dude

    SF11Dude

    Joined:
    Nov 8, 2018
    Posts:
    13
    Found it (json string)...it is working and I can see the data in the console!
    I have been reading the manual, forums, watching videos and changing my code for 3 weeks.
    Now I have to work on a loop and displaying the data appropriately on the screen.
    It is great someone knowledgeable stepped up and solved the problem.

    You are awesome. THANK YOU!!!
     
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Glad I could help. Good luck with your game!