c#的JSON反序列化

eehinjor2 2020-11-04 07:19:43
定义了一个
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可视化工具”,显示出来的内容是对的。

请教各位。
...全文
1919 点赞 收藏 5
写回复
5 条回复
正怒月神 2020年11月05日
复制json,打开vs,编辑--选择性黏贴--将json粘贴为类。 然后产生的class,就可以直接使用了。
回复 点赞
ziqi0716 2020年11月05日
用的这个工具:https://www.sojson.com/json2csharp.html 然后自己改下类名啥的
回复 点赞
ziqi0716 2020年11月05日

public class WxFile
{
    /// <summary>
    /// 
    /// </summary>
    public string fileid { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string file_name { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string spaceid { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string fatherid { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string file_size { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string ctime { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string mtime { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string file_type { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string file_status { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string create_userid { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string update_userid { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string sha { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string md5 { get; set; }
}

public class File_list
{
    /// <summary>
    /// 
    /// </summary>
    public List<WxFile> item { get; set; }
}

public class FileListType
{
    /// <summary>
    /// 
    /// </summary>
    public int errcode { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string errmsg { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string has_more { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string next_start { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public File_list file_list { get; set; }
}

回复 点赞
劲疯舞 2020年11月05日
引用 4 楼 正怒月神 的回复:
复制json,打开vs,编辑--选择性黏贴--将json粘贴为类。 然后产生的class,就可以直接使用了。
用了这么久的vs现在才发现这个功能
回复 点赞
eehinjor2 2020年11月04日
又仔细看了下,好像是数据定义错了,但是不知道怎么解决。在这个格式里面 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"
}]
}
}
回复 点赞
发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告