定義了一個
public class FileListType
{
public int errcode;
public string errmsg;
public string has_more;
public string next_start;
public WxFile[] file_list;
}
public class WxFile
{
public string fileid;
public string file_name;
public string spaceid;
public string fatherid;
public string file_size;
public string ctime;
public string mtime;
public string file_type;
public string file_status;
public string create_userid;
public string update_userid;
public string sha;
public string md5;
}
然后資料為:
string s = "{"+
"\"errcode\": 0,"+
"\"errmsg\": \"ok\","+
"\"has_more\": \"HAS_MORE\","+
"\"next_start\": \"NEXT_START\","+
"\"file_list\": " +
"{"+
"\"item\": [{"+
"\"fileid\": \"FILEID1\","+
"\"file_name\": \"FILE_NAME1\","+
"\"spaceid\": \"SPACEID\","+
"\"fatherid\": \"FATHERID\","+
"\"file_size\": \"FILE_SIZE\","+
"\"ctime\": \"CTIME\","+
"\"mtime\": \"MTIME\","+
"\"file_type\": \"FILE_TYPE\"," +
"\"file_status\": \"FILE_STATUS\"," +
"\"create_userid\": \"CREATE_USERID\"," +
"\"update_userid\": \"UPDATE_USERID\"," +
"\"sha\": \"SHA\"," +
"\"md5\": \"MD5\"}, " +
"{\"fileid\": \"FILEID2\"," +
"\"file_name\": \"FILE_NAME2\"," +
"\"spaceid\": \"SPACEID\"," +
"\"fatherid\": \"FATHERID\"," +
"\"file_size\": \"FILE_SIZE\"," +
"\"ctime\": \"CTIME\"," +
"\"mtime\": \"MTIME\"," +
"\"file_type\": \"FILE_TYPE\"," +
"\"file_status\": \"FILE_STATUS\"," +
"\"create_userid\": \"CREATE_USERID\"," +
"\"update_userid\": \"UPDATE_USERID\"," +
"\"sha\": \"SHA\"," +
"\"md5\": \"MD5\"}]" +
"}}";
反序列化的陳述句為:
FileListType objs = JsonConvert.DeserializeObject<FileListType>(s);
foreach (var file in objs.file_list)
{
textBox1.Text = textBox1.Text + file.fileid;
}
在第一句DeserializeObject時就出錯了
Newtonsoft.Json.JsonSerializationException:“Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'WindowsFormsApp1.WxFile[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'file_list.item', line 1, position 100.”
再除錯時,針對字串 s ,選擇“JSON可視化工具”,顯示出來的內容是對的。
請教各位。
uj5u.com熱心網友回復:
又仔細看了下,好像是資料定義錯了,但是不知道怎么解決。在這個格式里面 item和file_list 是什么關系?{
"errcode": 0,
"errmsg": "ok",
"has_more": "HAS_MORE",
"next_start": "NEXT_START",
"file_list": {
"item": [{
"fileid": "FILEID1",
"file_name": "FILE_NAME1",
"spaceid": "SPACEID",
"fatherid": "FATHERID",
"file_size": "FILE_SIZE",
"ctime": "CTIME",
"mtime": "MTIME",
"file_type": "FILE_TYPE",
"file_status": "FILE_STATUS",
"create_userid": "CREATE_USERID",
"update_userid": "UPDATE_USERID",
"sha": "SHA",
"md5": "MD5"
}, {
"fileid": "FILEID2",
"file_name": "FILE_NAME2"
}]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/203048.html
標籤:C#
