Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# JSON After parsing a value an unexpected character was encountered

Tags:

json

c#

parsing

i'm reading Json from a web api, with Newtonsoft.Json

most of their page are fine.

But some of them may contain some special character which fail my parsing.

Code is like:

WebClient client = new WebClient();
String strJson = client.DownloadString(url);

JObject jObject = JObject.Parse(strJson );

then i try to convert to UTF8 it still no work:

WebClient client = new WebClient();
String strJson = client.DownloadString(url);
byte[] utf8Bytes = Encoding.UTF8.GetBytes(strJson);
string safeJsonStr= Encoding.UTF8.GetString(utf8Bytes);
JObject jObject = JObject.Parse(safeJsonStr);

please help!

thanks

like image 280
Benny Ae Avatar asked Nov 01 '22 10:11

Benny Ae


1 Answers

well, finally i find a way.

i see there are some special language, and i feel i need a decode or encode.

then finally i find this works:

            client.Encoding = System.Text.Encoding.UTF8;

            strJson = client.DownloadString(url); 
like image 93
Benny Ae Avatar answered Nov 10 '22 16:11

Benny Ae