Benny Ae
Benny Ae

Reputation: 2016

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

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

Upvotes: 5

Views: 13196

Answers (1)

Benny Ae
Benny Ae

Reputation: 2016

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); 

Upvotes: 2

Related Questions