简体   繁体   中英

Can't parse json from Resources

In resources I have json file with next content:

{
"EU": [
    "Germany",
    "Ukraine",
    "United Kingdom",
    "Hungary"
 ]
}

I want to deserialize it into Dictionary<string,List<string>>

I've tried next :

var json = Encoding.UTF8.GetString(Resources.regionGroups);//Resources.regionGroups return byte[]

return JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(json);

But every time I get exception as variable json is in incorect json format.

What can cause this? I've tried the same deserialization but with jsonString as hard-coded and it works.


Detailed exception message :

Unexpected character encountered while parsing value: . Path '', line 0, position 0.


UPDATE :

After removing all spaces

var json = Regex.Replace(Encoding.UTF8.GetString(Resources.regionGroups), "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1");

from string I have next one

"{\"EU\":[\"Germany\",\"Ukraine\",\"United Kingdom\",\"Hungary\"]}"

which also reproduce exception.

Well @AmitKumarGhosh was right about encoding, as I think.

So I've tried to change type of my json file in the Resources. I've changed it from binary to text file and this helps.

So parsing now is very simple :

JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(Resources.regionGroups);

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