中的对象的脚本集合。
我正在尝试对json字符串进行挑选。
这是我的代码:
[System.Serializable]
public class SharedWorlds
{
public int worldId { get; set; }
public System.DateTime uploaded { get; set; }
public string username { get; set; }
public string levelName { get; set; }
public string gameVersion { get; set; }
public string description { get; set; }
public string filename { get; set; }
public string screenshot1 { get; set; }
public string screenshot2 { get; set; }
public string userTag { get; set; }
public string userURL { get; set; }
public double price { get; set; }
public int nrDownload { get; set; }
public int votes { get; set; }
}
[System.Serializable]
public class Record {
public List<SharedWorlds> record;
}
try {
SDE3D _webService = new SDE3D();
result= _webService.GetMassiveWorldsList ();
var records = JsonUtility.FromJson<Record>(result);
}
catch(System.Exception ex) {
Debug.Log (ex.Message.ToString ());
}
这是我有效的JSON(这里有两个记录,但我想每次发送许多记录(。
[
{
"worldId": 5,
"uploaded": "/Date(1524875719000)/",
"username": "quik",
"levelName": "Station",
"gameVersion": "1.0.1",
"description": "iwoeijksf",
"filename": "0000003.dat",
"screenshot1": "0000003a.png",
"screenshot2": "0000003b.png",
"userTag": "",
"userURL": "",
"price": 0,
"nrDownload": 5,
"votes": 5
},
{
"worldId": 4,
"uploaded": "/Date(1524875659000)/",
"username": "aksio",
"levelName": "Garage",
"gameVersion": "1.0.1",
"description": "Adlkld",
"filename": "0000003.dat",
"screenshot1": "0000003a.png",
"screenshot2": "0000003b.png",
"userTag": "",
"userURL": "",
"price": 0,
"nrDownload": 4,
"votes": 4
}
]
我遇到了错误:
"参数exception:json必须代表对象类型。"
我很确定错误在此代码行中:
var records = JsonUtility.FromJson<Record>(result);
如何对JSON对象的数组进行序列化?
谢谢
,因为您的JSON数据不是Record
。这是SharedWorld
s的集合。这样的东西:
var sharedWorlds = JsonUtility.FromJson<SharedWorld[]>(result);
或也许:
var sharedWorlds = JsonUtility.FromJson<List<SharedWorld>>(result);
您可以创建Record
:
var record = new Record { record = sharedWorlds };
如果JSON需要在Record
中进行估算,则需要以Record
对象的格式:
{
"record":
[
/* the rest of your JSON within the square brackets */
]
}
然后是Record
:
var record = JsonUtility.FromJson<Record>(result);
*旁注:您的类和可变名称以及所使用的复数化确实令人困惑。其语义可能不会使您的调试更容易为您带来。
您可以进入json