LitJson: Instance of JsonData is not a dictionary Possible cause of error

If there is such a string: {"name":"mobileinput","data":"{\"msg\":\"READY\",\"id\":0}"}

Direct use of the following parsing process may throw an exception of the title

code show as below

static void Main(string[] args)
{
    //J:\Test\json.txt 是包含这段字符串的txt文本
    FileStream fs = File.Open(@"J:\Test\json.txt", FileMode.Open);
    StreamReader reader = new StreamReader(fs);
    string json = reader.ReadToEnd();
    Console.WriteLine(json);
    JsonData data = JsonMapper.ToObject(json);
    JsonData response = data["data"];
    string msg = response["msg"].ToString();
    Console.WriteLine(msg);
}

Then the exception is

Encountering such a situation, I can't understand it. So it can only be solved by the ToString() method of the string.

The modified code is as follows

static void Main(string[] args)
{
    FileStream fs = File.Open(@"J:\Test\json.txt", FileMode.Open);
    StreamReader reader = new StreamReader(fs);
    string json = reader.ReadToEnd();
    Console.WriteLine(json);
    JsonData data = JsonMapper.ToObject(json);
    //多一次转换为string的过程
    string responseStr = data["data"].ToString();
    //然后将这个string解析为json
    JsonData response = JsonMapper.ToObject(responseStr);
    string msg = response["msg"].ToString();
    Console.WriteLine(msg);
}

If you run it like this, you can correctly parse out the value of msg as "READY".

I don't know if anyone has encountered such a BUG. . or is it just me

Guess you like

Origin blog.csdn.net/DoyoFish/article/details/102553943