Home > Net >  C # the JSON serialization
C # the JSON serialization

Time:11-05

Defines a
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 count;
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;
}

Then the data is:
String s={" + "
Errcode "" \ ": 0, +"
Errmsg "" \ ", \ "ok ", "+
"" has_more \ ": " has_more \ ", "+
"" next_start \ ": " next_start \ ", "+
"" file_list \ ":" +
{" + "
The item \ '\' ": [{" +
FILEID1 "" count \ ": " \ ", "+
FILE_NAME1 "" file_name \ ": " \ ", "+
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 \}, "" +
"{\ 'count " : \ "FILEID2 ", "+
FILE_NAME2 "" file_name \ ": " \ ", "+
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 \ "}] + "
"}} ";

Deserialization statement is:
FileListType objs=JsonConvert. DeserializeObject (s);


Foreach (var file in objs. File_list)
{
TextBox1. Text=textBox1. Text + file. Count;
}

Made a mistake in the first sentence DeserializeObject
Newtonsoft. Json. JsonSerializationException: "always deserialize the current Json object (e.g. {" name" : "value"}) into the 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 an integer, not a collection type like the an array or List) That can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize the from a JSON object.
The Path 'file_list. Item, line 1, position 100. "

To debug, in view of the string s, choose "JSON visualization tools," displayed is correct, the content of the

Consult everybody,

CodePudding user response:

Look closely again, as the data definition is wrong, but don't know how to solve, in this format item and file_list what is the relationship?

{
"Errcode" : 0,
"Errmsg" : "ok,"
"Has_more" : "has_more,"
"Next_start" : "next_start,"
"File_list" : {
"Item" : [{
"Count" : "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"
}, {
"Count" : "FILEID2,"
"File_name" : "FILE_NAME2
"}]
}
}
  •  Tags:  
  • C#
  • Related