0

I need to serialize a json list. Here is my json model :

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class jsonModel : MonoBehaviour {

    [System.Serializable]
    public class Meta {
        public int total_rapports;
    }

    [System.Serializable]
    public class PhotoPhaseContentRapportTravaux {
        public string photo_path = "";
        public string phase_comment = "";
    }

    [System.Serializable]
    public class PhaseContentRapportTravaux {
        public string phase_comment = "";
        public string photo_path = "";
        //public List <PhotoPhaseContentRapportTravaux> photoPhaseRapportTravaux;

    }


    [System.Serializable]
    public class PhaseRapportTravaux {
        public string phaseName;
        public List <PhaseContentRapportTravaux> phaseContentRapportTravaux;

    }

    [System.Serializable]
    public class RapportTravaux {
        //public CredentialsRapportTravaux credentialsRapportTravaux;
        public string prenom_cdt="";
        public string nom_cdt="";
        public string nom_chantier="";
        public string nom_usine="";
        public string annee_chantier="";

        public List <PhaseRapportTravaux> phaseRapportTravaux;
        public int id;
    }

    [System.Serializable]
     public class RootObject {
         public Meta meta;
         public List <RapportTravaux> rapport_travaux;

    }

    static public RootObject EDFRapports;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    static public void SaveJson()
    {
        string output_total_clients = JsonUtility.ToJson(EDFRapports);
        File.WriteAllText(Path.Combine(Application.persistentDataPath, "database.json"), output_total_clients);
    }

    static public void OpenJSON() {
        string jsonRapports = File.ReadAllText(Path.Combine(Application.persistentDataPath, "database.json"));
        EDFRapports = JsonUtility.FromJson<jsonModel.RootObject>(jsonRapports);
    }
}

Now here is my function which try to set contents in the PhotoPhaseContentRapportTravaux class adding photo_path and phase_comment.

public void SavePhase() {

        jsonModel.OpenJSON ();

        try {
            jsonModel.EDFRapports.rapport_travaux[phasesContentStatus.IDCurrentPhase].phaseRapportTravaux.Add(new jsonModel.PhaseRapportTravaux() {
                    phaseName = phaseName.text
                });

            jsonModel.EDFRapports.rapport_travaux[phasesContentStatus.IDCurrentPhase].phaseRapportTravaux[phasesContentStatus.IDCurrentPhase].phaseContentRapportTravaux[phasesContentStatus.IDCurrentPhase].phase_comment= "Some comments";
            jsonModel.EDFRapports.rapport_travaux[phasesContentStatus.IDCurrentPhase].phaseRapportTravaux[phasesContentStatus.IDCurrentPhase].phaseContentRapportTravaux[phasesContentStatus.IDCurrentPhase].photo_path = "Photo Path here";

        }  catch {
            Debug.Log("error saving content phase");
        }

        //Save
        jsonModel.SaveJson();

    }

However I always get the catch error... I don't understand why I can not serialize this json model. Any idea ? Thanks a lot

Silvering
  • 756
  • 1
  • 6
  • 33
  • Without posting your json, it is impossible to help you. – Programmer May 01 '17 at 11:57
  • sorry here is it : https://gist.github.com/Silvering/559fb28b175cd08b10ec5ccecea260c8 – Silvering May 01 '17 at 12:03
  • Paste on this [link](http://json2csharp.com/) and it will generate the right classes for it. Remove `{ get; set; }` from the generated classes then `JsonUtility.FromJson` should work when you use it on the `RootObject` class that is generated. Don't forget to add `[System.Serializable]`. See the duplicated answer for more information. – Programmer May 01 '17 at 12:07
  • Thanks for your reply. The link you gave me generate this: https://gist.github.com/Silvering/f3bd7cad66fcd2ef37183f810cbc0ca5 However now how can I access to "path_comment" and "photo_path" ? Here is my json: https://gist.github.com/Silvering/c13df3f0f445ce96475c750b43fd3d71 – Silvering May 01 '17 at 15:05

0 Answers0