我需要序列化一个json列表。这是我的json模型:
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);
}
}
现在这是我的函数,它尝试在PhotoPhaseContentRapportTravaux类中添加photo_path和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();
}
但是我总是遇到catch错误...我不明白为什么我不能序列化这个json模型。任何的想法 ?非常感谢