0

手順1.最初のメッセージをdictionary1(receivedDict)にシリアル化解除します

手順2.dictionary1["data"]をdictionary2(receivedDict2)にシリアル化解除します

2番目のレイヤーに別のシリアル化された配列が含まれている場合は常に失敗します。そうでない場合は機能します。3番目のステップはdictionary2["replaceArray"]のシリアル化を解除することですが、エラーのためにその段階に到達していません。

Piccy:

ここに画像の説明を入力してください

コード:

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); }

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;
}

つまり、基本的には、最初のメッセージが埋め込まれたシリアル化された配列を含んでいても、問題なくアンシリアル化されますが、まったく同じことをもう一度実行しようとすると失敗します。これを修正するにはどうすればよいですか。

4

1 に答える 1

1

これdata 文字列であり、JSON オブジェクトではなく、JSON オブジェクトの文字列表現であり、大きな違いがあるためです ("の先頭と末尾にある に注意してdataください)。

私の知る限り、JSON オブジェクトを文字列に逆シリアル化することはできません。またはとして逆シリアル化するJObjectDictionary<string, object>、(データが変更されない場合の最良のオプション) このクラスを作成して、それに逆シリアル化する必要があります。

于 2012-04-22T15:09:30.653 に答える