简体   繁体   English

Json反序列化字符串错误

[英]Json Deserialize string error

private void ExmpDatasave()
    {
        DataSet dataSet = new DataSet("dataSet");
         dataSet.Namespace = "NetFrameWork";
         DataTable table = new DataTable();
         DataColumn idColumn = new DataColumn("id", typeof(int));
         idColumn.AutoIncrement = true;

         DataColumn itemColumn = new DataColumn("item");
         table.Columns.Add(idColumn);
         table.Columns.Add(itemColumn);
        dataSet.Tables.Add(table);

        for (int i = 0; i < 2; i++)
        {
            DataRow newRow = table.NewRow();
            newRow["item"] = "item " + i;
            table.Rows.Add(newRow);
        }

        dataSet.AcceptChanges();

        string json = JsonConvert.SerializeObject(dataSet, Formatting.None);
         Helper.WriteToJson(json);
    }

    private void ExmpDataread()
    {

        using (StreamReader r = new StreamReader("exmp.json"))
        {
            string json = r.ReadToEnd();

            var myJSONString = json.ToString();

            DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);
            DataTable dataTable = dataSet.Tables["Table1"];
            Console.WriteLine(dataTable.Rows.Count);
        }
    }

Hey its my brief code ExmpDatasave() is used to save a entire data in json works fine; 嘿,我的简短代码ExmpDatasave()用于将整个数据保存在json中,效果很好; ExmpDataread gives error: ExmpDataread给出错误:

Unexpected end when reading DataSet. 读取DataSet时意外结束。 Path '', line 1, position 78. 路径'',第1行,位置78。

My json values "{\\"Table1\\":[{\\"id\\":0,\\"item\\":\\"item 0\\"},{\\"id\\":1,\\"item\\":\\"item 1\\"}]}" 我的json值"{\\"Table1\\":[{\\"id\\":0,\\"item\\":\\"item 0\\"},{\\"id\\":1,\\"item\\":\\"item 1\\"}]}"
Please help me to figure out. 请帮我弄清楚。 Thank you in advance.. 先感谢您..

I experience this exact issue and although I wasn't savvy enough to resolve the issue I was able to work around it and for the better I think. 我遇到了这个确切的问题,尽管我不足以解决这个问题,但我能够解决这个问题,并且我认为更好。

I decided to use Newtonsoft.Json.Linq; 我决定使用Newtonsoft.Json.Linq; to get similar results 得到类似的结果

            dynamic json = JsonConvert.DeserializeObject(reader.ReadToEnd());
            JObject js = JObject.Parse(json);
            Console.Writeline(js["Table1"].Count);

I wouldn't consider this a full answer as I was never able to figure out why It is giving the error when everything says it shouldn't be. 我不会认为这是一个完整的答案,因为我始终无法弄清为什么当一切都说不应该这样做时会给出错误。 But I was rather happy to find this path so I hope it helps 但是我很高兴找到这条路,希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM