对不起我的英语,好吧,我正在尝试反序列化UNITY和C#上的json文件, 当我得到一个对象列表(int,string,float,bool inside)时,一切正常,没有问题, JSON BASIC STRUCTURE
但是当我得到一个列表,其元素中有另一个列表出现问题时,我的意思是列在另一个列表中 json second structure
我的c#代码,我加载了json文件,(我得到了这个配置,因为它适用于Android和统一编辑器)
public void loadjsonfile(string jsonfilename)
{
TextAsset file = Resources.Load(jsonfilename) as TextAsset;
jsonData = file.ToString ();
}
public List<club> findClubsIn(string jsonFilename, string leagueName)
{
List<club> clubFound = new List<club>();
if(jsonFilename.Equals("european_club"))
{
loadjsonfile (jsonFilename);
europeanClub uefaclubs = JsonUtility.FromJson<europeanClub> (jsonData);
List<club> clubList = uefaclubs.europeanClubs;
foreach (club cl in clubList)
{
if (cl.league.Equals (leagueName))
{
//Debug.Log (cl.name);
clubFound.Add (cl);
}
}
return clubFound;
}
clubFound.Clear ();
return clubFound;
}
&#13;
现在,在同一个班级我得到了这个
[System.Serializable]
public class cab
{
public float domestic_league;
public float domestic_cup;
public float domestic_s_cup;
public float euro_champ_cup;
public float euro_league;
public float euro_s_cup;
public float inter_club_champ;
}
[System.Serializable]
public class kits
{
public string h_m_color;
public string h_s_color;
public string a_m_color;
public string a_s_color;
public string kit_sponsor;
public string kit_supplier;
public string sp_contract;
public string supp_contract;
}
[System.Serializable]
public class transf
{
public float transfer_budget;
public float wage_budget;
public float foreign_p_bias;
public float transfer_activity;
public bool only_national;
}
[System.Serializable]
public class fac
{
public string stadium;
public float stadium_cap;
public string training_facilities;
}
[System.Serializable]
public class club
{
public int club_id;
public string sprite_id;
public string name;
public string confederation;
public string nation;
public string league;
public float overall;
public float international_rep;
public float domestic_rep;
public string financial_state;
public float youth_policy;
public string second_team;
public float market_value;
public string manager;
public float squad_size;
public string continental_comp;
public string continental_s_cup;
public List<fac> facilities;
public List<transf> transfers;
public List<kits> kit;
public List<cab> cabinet;
}
[System.Serializable]
public class europeanClub
{
public List<club> europeanClubs;
}
&#13;
但是当我尝试做到这个问题时,我真的不知道自己做得不好,希望你能提前帮助,谢谢。
JsonDataHandler data = new JsonDataHandler ();
List<club> clubsFound = data.findClubsIn ("european_club","Premier League");
List<fac> fac = new List<fac> ();
foreach(club cl in clubsFound)
{
fac.Add (cl.facilities);
}
foreach(fac fs in fac)
{
Debug.Log (fs.stadium);
}
&#13;
错误
Assets / Scripts / scrollClub.cs(42,9):错误CS1502:最好的重载方法匹配`System.Collections.Generic.List.Add(fac)&#39;有一些无效的论点
答案 0 :(得分:0)
“cl.facilities”是List。因此,将其添加到列表时会显示错误。您可以将其更改为“fac.AddRange(cl.facilities)”以改为附加到列表中。