Can't read a json data

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I'm working in Unity and trying to read a Json data with LitJson. It works fine when I read the tournamentCode data but I don't get to read the "desc"{"en": "" } data. Can anybody help me?

This is the Json data:

{
  "data": {
    "server_time": 1554749832667,
    "leaderboardEnrollment": {
      "score": 12,
      "data": {
        "nickname": ""
      },
      "createdAt": 1554476229071,
      "updatedAt": 1554476302054,
      "tCode": "T8",
      "tRank": 1,
      "tClaimedAt": 0,
      "tNotifiedAt": 0
    },
    "versionId": 2,
    "tournamentTimetable": {
      "tRegistrationStart": 1554475800000,
      "tState": "COMPLETE",
      "tPlayEnd": 1554479100000,
      "tRegistrationEnd": 1554477300000,
      "tPlayStart": 1554475800000
    },
    "tournamentConfigs": [
      {
        "tournamentCode": "T8",
        "description": {
          "name": {
            "en": "TORNEO8"
          },
          "desc": {
            "en": ""
          }
        },
        "customJson": {},
        "payoutRules": [],
        "entryFee": {}
      }
    ],
    "enrolled": true
  },
  "status": 200
}

And this is my code:

public class BC_TData : MonoBehaviour
{
    public GameObject Button_Template;

    public string escena = "08_MenuInicio";
    private string ScorePlayerCH;

    private BrainCloudWrapper _bc;

    private readonly List<BC_PlayerInfo> lblist = new List<BC_PlayerInfo>();
    private readonly List<BC_PlayerInfo> tlist = new List<BC_PlayerInfo>();
    private readonly List<BC_PlayerInfo> dlist = new List<BC_PlayerInfo>();

    private int versionId = 2;

    public Text TituloTorneo;
    public Text FechaComienzoPartida;
    public Text FechaFinalizacionPartida;
    public Text FechaComienzoUnirse;
    public Text FechaFinalizacionUnirse;
    public Text DescripcionTorneo;
    public Text UnidoTorneo;

    public GameObject ventana1
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

 

I recommend using Unity's built-in solution to JSON, JsonUtility.FromJson() and it will autofill the following class scheme(from http://json2csharp.com/#), which should be easier to handle and not require an external library. Of course, this is an alternative, perhaps you may prefer to keep using LitJson, this should read "desc": "en". to implement this you would use...

    public class JsonHelper : MonoBehaviour
    {
      public RootObject jsonToRootObject; //access this with your for/foreach loops
      void start(){
      jsonToRootObject = JsonUtility.FromJson<RootObject>(place your json string here);
      }
    }



[Serializable]
public class Data2
{
    public string nickname;
}
[Serializable]
public class LeaderboardEnrollment
{
    public int score;
    public Data2 data;
    public long createdAt;
    public long updatedAt;
    public string tCode;
    public int tRank;
    public int tClaimedAt;
    public int tNotifiedAt;
}
[Serializable]
public class TournamentTimetable
{
    public long tRegistrationStart;
    public string tState;
    public long tPlayEnd;
    public long tRegistrationEnd;
    public long tPlayStart;
}
[Serializable]
public class Name
{
    public string en;
}
[Serializable]
public class Desc
{
    public string en;
}
[Serializable]
public class Description
{
    public Name name;
    public Desc desc;
}
[Serializable]
public class CustomJson
{
}
[Serializable]
public class EntryFee
{
}
[Serializable]
public class TournamentConfig
{
    public string tournamentCode;
    public Description description;
    public CustomJson customJson;
    public List<object> payoutRules;
    public EntryFee entryFee;
}
[Serializable]
public class Data
{
    public long server_time;
    public LeaderboardEnrollment leaderboardEnrollment;
    public int versionId;
    public TournamentTimetable tournamentTimetable;
    public List<TournamentConfig> tournamentConfigs;
    public bool enrolled;
}
[
                                                    
                                                    
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community