当我使用json读取json文件时,我收到了错误。 [英] I got the error When I read json file using json.

查看:640
本文介绍了当我使用json读取json文件时,我收到了错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我读json文件时出现两个错误。



我的代码是

Hi,
I got two errors When I read json file.

My code is

public class JsonData
   {
       public string Title { get; set; }
       public string Description { get; set; }
       public string Link { get; set; }
      
   }







using (StreamReader r = new StreamReader(filename))
                   {
                       string json = r.ReadToEnd();
                       List<JsonData> datas = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JsonData>>(json);

                       var data = from c in datas
                           group c by
                               new
                               {
                                   c.Title,
                                   c.Link,
                                   c.Description

                               }
                           into gac
                           select new JsonData()
                           {
                               Title = gac.Key.Title,
                               Link = gac.Key.Link,
                               Description = gac.Key.Description

                           };
                       foreach (var jsonData in data)
                       {
                           Console.WriteLine("Title:" + jsonData.Title);
                       }

                   }





我收到了这两个错误



I got these two errors

After parsing a value an unexpected character was encountered: T. Path '[644].Description', line 5160, position 44.




Bad JSON escape sequence: \S. Path '[147].Description', line 1184, position 247.



请帮帮我。

谢谢。


Please help me.
Thanks.

推荐答案





你能在这里发布你的json字符串吗?通过阅读错误,我建议您在反序列化之前删除所有\字符。



尝试
Hi,

Can you post your json string here ? By reading at the error, I would suggest you remove all the "\" character before you deserialize it.

Try
json = json.Replace(@"\", "");

在反序列化过程之前。



问候,

Praneet

before the deserialization process.

Regards,
Praneet


您好,



尝试使用自定义序列化功能..



以下是用于反序列化的模板类



Hi,

Try to use a custom serialize function ..

Below is the Template class used for Deserialize

<pre lang="cs">public static T Deserialize&lt;T&gt;(string json) {
           var obj = Activator.CreateInstance&lt;T&gt;();
           using (var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json))) {
               var serializer = new DataContractJsonSerializer(obj.GetType());
               obj = (T) serializer.ReadObject(memoryStream);
               return obj;
           }
       }</pre>









相应的模板功能可以如下所示使用...







The respective template function can be used as shown below...

StudentList = Deserialise<list>< Student >> (JsonString);

</list>


hi,

我发现了问题和解决方案。 />

I found the problem and soluation.
After parsing a value an unexpected character was encountered




Bad JSON escape sequence: \S





来自\和\特殊字符的这两个错误。



我这样替换了



these two errors from \ and \" special characters.

I replaced like this

json = json.Replace(@"\""", "s");



之后


after that

json = json.Replace(@"\", "");



如果你再次替换第一个\然后\在json字符串中你就会出错。


if you replace first \ then \" in a json string again you got error.


这篇关于当我使用json读取json文件时,我收到了错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆