简体   繁体   中英

How to parse JSON with corrupted string?

I generate JSON from many things, images, video, text editor, etc. And sometimes the editor generates invalid json data, such as whitespace.

On this example json.parse doesn't work cause of whitespace.

[{"data":"yLI3B33wguU","modalityId":17,"position":2},{"mediaId":"55","modalityId":26,"position":0},{"text":"<p>J&#39;apprends la ponceuse</p>","modalityId":28,"position":3},{"text":"<p>Ma shopping list :</p><ul> <li>perceuse</li>   <li>peinture</li>   <li>pinceau</li>    <li>du matos</li>   <li>...</li></ul>","modalityId":28,"position":1}]

Is there any method in JavaScript or C# to solve all corrupted data for json.Parse ?

When I give the data in single quotes to JSON.parse() it worked without any errors.

JSON.parse('[{"data":"yLI3B33wguU","modalityId":17,"position":2},{"mediaId":"55","modalityId":26,"position":0},{"text":"<p>J&#39;apprends la ponceuse</p>","modalityId":28,"position":3},{"text":"<p>Ma shopping list :</p><ul> <li>perceuse</li>   <li>peinture</li>   <li>pinceau</li>    <li>du matos</li>   <li>...</li></ul>","modalityId":28,"position":1}]');

Just make sure your JSON is a string before passing to JSON.parse();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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