Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将3层数组取消序列化为字典_C#_Json_Json.net - Fatal编程技术网

C# 将3层数组取消序列化为字典

C# 将3层数组取消序列化为字典,c#,json,json.net,C#,Json,Json.net,第一步。取消将第一条消息序列化到字典1(ReceivedDisct) 第二步。将字典1[“数据”]取消序列化为字典2(ReceivedDictionar2) 每当第二层包含另一个序列化数组时,它就会失败。如果它不起作用,它就会起作用。第三步是取消序列化dictionary2[“replaceArray”],但由于错误,我还没有达到那个阶段 皮西: 守则: CGlobals.output("Received a message with length: " + _message.Length);

第一步。取消将第一条消息序列化到字典1(ReceivedDisct)

第二步。将字典1[“数据”]取消序列化为字典2(ReceivedDictionar2)

每当第二层包含另一个序列化数组时,它就会失败。如果它不起作用,它就会起作用。第三步是取消序列化dictionary2[“replaceArray”],但由于错误,我还没有达到那个阶段

皮西:

守则:

CGlobals.output("Received a message with length: " + _message.Length);
Console.WriteLine("message Contains : " + _message);

//Unserialize main message
Dictionary<string, string> receivedDict = new Dictionary<string, string>();
try
{
    receivedDict = CJsonStuff.unserializeDict(_message);
}
catch (Exception ex) { return retError(1, ex.Message,false); }
if (receivedDict.Count < 1) return retError(2, "",false);

//List all elements for debugging.
string temp = "";
foreach (KeyValuePair<string, string> item in receivedDict)
{
    temp += item.Key + " : " + item.Value + "\n";
}
Console.WriteLine("\n" + temp + "\n");

//Parse jobID
int jobID = -1;
try
{
    jobID = Int32.Parse(receivedDict["jobID"]);
}
catch (Exception ex) { return retError(3, ex.Message,false); }

//Parse alteredID
int alteredID = -1;
bool isAltered = false;
try
{
    Dictionary<string, string> receivedDict2 = new Dictionary<string, string>();
    Console.WriteLine("Unserializing : "+receivedDict["data"]);
    receivedDict2 = CJsonStuff.unserializeDict(receivedDict["data"]); //Error occurs here
    Console.WriteLine("Unserialized data");

    //Show all elements for debugging.
    string temp2 = "";
    foreach (KeyValuePair<string, string> item in receivedDict2)
    {
        temp2 += item.Key + " : " + item.Value + "\n";
    }
    Console.WriteLine("\n" + temp2 + "\n");

    if (receivedDict2.ContainsKey("alteredID"))
    {
        alteredID = Int32.Parse(receivedDict2["alteredID"]);
        isAltered = true;
    }
}
catch (Exception ex) { Console.WriteLine(ex.Message); return retError(6, ex.Message, false, jobID); }
CGlobals.output(“收到一条长度为“+”的消息);
Console.WriteLine(“消息包含:”+_消息);
//取消序列化主消息
Dictionary ReceivedDictionary=新字典();
尝试
{
receivedDict=CJsonStuff.unserializeDict(\u消息);
}
catch(异常ex){return retError(1,ex.Message,false);}
如果(receivedDict.Count<1)返回retError(2,“,false);
//列出要调试的所有元素。
字符串temp=“”;
foreach(ReceivedAct中的KeyValuePair项)
{
temp+=item.Key+“:“+item.Value+”\n”;
}
Console.WriteLine(“\n”+temp+”\n”);
//解析作业ID
int-jobID=-1;
尝试
{
jobID=Int32.Parse(receivedDict[“jobID”]);
}
catch(Exception ex){return retError(3,ex.Message,false);}
//解析交替
int alteredID=-1;
bool isAltered=false;
尝试
{
Dictionary receivedDict2=新字典();
控制台.WriteLine(“非序列化:+receivedDict[“数据”);
receivedDict2=CJsonStuff.unserializeDict(receivedDict[“data”]);//此处发生错误
Console.WriteLine(“未序列化数据”);
//显示用于调试的所有元素。
字符串temp2=“”;
foreach(receivedDict2中的KeyValuePair项)
{
temp2+=item.Key+“:“+item.Value+”\n”;
}
Console.WriteLine(“\n”+temp2+”\n”);
如果(已收到添加2.ContainsKey(“已更改”))
{
alteredID=Int32.Parse(receivedDict2[“alteredID”]);
isAltered=真;
}
}
catch(异常ex){Console.WriteLine(ex.Message);返回retError(6,ex.Message,false,jobID);}
CJSonStuff:

public static Dictionary<string, string> unserializeDict(string thestring)
{
    Dictionary<string, string> dict = new Dictionary<string, string>();
    dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(thestring);
    return dict;
}
publicstaticdictionary unserializeDict(字符串)
{
Dictionary dict=新字典();
dict=JsonConvert.DeserializeObject(字符串);
返回命令;
}

因此,基本上,它可以很好地取消序列化第一条消息,即使它包含一个嵌入的序列化数组,但尝试再次执行完全相同的操作失败。。如何解决这个问题?

这是因为
数据
是一个字符串,它不是JSON对象,而是JSON对象的字符串表示,这是一个很大的区别(请注意
数据的开头和结尾处的

据我所知,您不能将JSON对象反序列化为字符串,而且这样做对我来说没有意义。您应该将其反序列化为
JObject
Dictionary
,或者(如果数据不变,最好的选项)为此创建一个类并反序列化为该类