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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Im a noob with json data

Discussion in 'Scripting' started by ReggieBeRetro, Nov 30, 2018.

  1. ReggieBeRetro

    ReggieBeRetro

    Joined:
    Aug 30, 2015
    Posts:
    73
    If anyone can help it would be much appreciated. Ive read a few tutorials and watched even more youtube videos and im still stuck. Here a little backgroud so im working on a history panel for my game to display what the player has done after games on the home panel. Im running into some issues while trying to load json data.
    For some reason I cant set the data to a list. I want to do a foreach loop to set and display data to a prefab ultimately I cant seam to get this right. If anyone can shed some light on this for me would be great.

    C# Script
    https://pastebin.com/H6VT2wK4

    JSON Database
    https://pastebin.com/ATMNNSAv
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please don't use external links. Either use Code tags from the ribbon bar above the text edit box (preferred) or the Upload a File button below.
     
  3. ReggieBeRetro

    ReggieBeRetro

    Joined:
    Aug 30, 2015
    Posts:
    73
    ok np.

    Here is the c# file

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.IO;
    7.  
    8.  
    9. public class RecentHistoryDisplay : MonoBehaviour
    10. {
    11.     private string historyDataProjectFilePath = "/StreamingAssets/historydata.json";
    12.     [System.Serializable]
    13.     public class HNode
    14.     {
    15.         public int _id;
    16.         public string historyTitle;
    17.         public string hType;
    18.         public int hId;
    19.         public int itemId;
    20.         public int itemMultipier;
    21.         public string claimStatus;
    22.     }
    23.     [System.Serializable]
    24.     public class HisData
    25.     {
    26.         public List<HNode> CurrentHistory;
    27.  
    28.     }
    29.     void Start()
    30.     {
    31.         LoadHistoryData();
    32.     }
    33.     public void LoadHistoryData()
    34.     {
    35.         string filePath = Application.dataPath + historyDataProjectFilePath;
    36.  
    37.         if (File.Exists(filePath))
    38.         {
    39.          
    40.             string dataAsJson = File.ReadAllText(filePath);
    41.            
    42.             HisData hdata = JsonUtility.FromJson<HisData>(dataAsJson);
    43.        
    44.             if (hdata.CurrentHistory.Count > 0) {
    45.                
    46.                 //Debug.Log(hdata.CurrentHistory[0]);
    47.                 foreach (HNode htemp in hdata.CurrentHistory)
    48.                 {
    49.                     Debug.Log(htemp.claimStatus);
    50.                 }
    51.             }
    52.          
    53.         }
    54.         else
    55.         {
    56.            HisData hData = new HisData();
    57.    
    58.         }
    59.     }
    60.   }
    61.  
    And the JSON Database.

    Code (JSON):
    1. {
    2.     "historyitems": [
    3.         {
    4.     "_id":"8484239823",
    5.     "historyTitle":"Item Unlocked",
    6.     "hType":"itemunlock",
    7.     "hId":"6375457765",
    8.     "itemId":"423423",
    9.     "itemMultiplier":"NoMultiplier",
    10.     "claimStatus":"Claimed"
    11.         },
    12.         {
    13.     "_id":"4344239823",
    14.     "historyTitle":"Match Won",
    15.     "hType":"matchwon",
    16.     "hId":"1643457765",
    17.     "itemId":"000001",
    18.     "itemMultiplier":"2",
    19.     "claimStatus":"Claimed"
    20.         },
    21.         {
    22.     "_id":"3475239823",
    23.     "historyTitle":"Xp Bonus",
    24.     "hType":"xpbonus",
    25.     "hId":"7123457765",
    26.     "itemId":"745623",
    27.     "itemMultiplier":"10",
    28.     "claimStatus":"NotClaimed"
    29.         },
    30.         {
    31.     "_id":"2775239823",
    32.     "historyTitle":"Mission Complete",
    33.     "hType":"completemission",
    34.     "hId":"9523457765",
    35.     "itemId":"000002",
    36.     "itemMultiplier":"0",
    37.     "claimStatus":"None"
    38.         },
    39.         {
    40.     "_id":"3475239823",
    41.     "historyTitle":"Mission Failed",
    42.     "hType":"failedmission",
    43.     "hId":"5323457765",
    44.     "itemId":"000003",
    45.     "itemMultiplier":"0",
    46.     "claimStatus":"None"
    47.         }
    48.     ]
    49. }
    50.  
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    And what is the issue? Please describe what you expect to happen, and what isn't working. FYI this is a handy online tool for JSON data https://jsoneditoronline.org/
     
  5. ReggieBeRetro

    ReggieBeRetro

    Joined:
    Aug 30, 2015
    Posts:
    73
    im not sure exactly. im not getting anything in the list. the count returns 0 when if (hdata.CurrentHistory.Count > 0) is checked.
     
  6. M4XXX

    M4XXX

    Joined:
    Aug 27, 2017
    Posts:
    10
    Your property is called CurrentHistory but in the Json it is called historyitems - the deserialiser doesn't know what to do with historyitems - so doesn't do anything!
    Just change one or the other.
     
  7. ReggieBeRetro

    ReggieBeRetro

    Joined:
    Aug 30, 2015
    Posts:
    73
    omg thank you. I was suck all day yesterday with that Its working now.