-1

I have this JSON file:

[
    {
        "param1": "someURL",
        "param2": "someURL2"
    },
    {
        "param1": "someURL3",
        "param2": "someURL4"
    }
]

JsonLint says that this JSON is valid.

Now I read this JSON file from Azure Storage download it to a stream and then encode it:

string content = Encoding.UTF8.GetString(memoryStream.ToArray());

Next I want to deserialize this to a list of objects:

List<MyParamObject> deserialized = JsonConvert.DeserializeObject<List<MyParamObject>>(text);

MyParamObject is a POCO with two attributes named Param1 and Param2

When I try to deserialize it I get the following error:

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

But I do give JsonConvert a string with a valid JSON what can I do?

Daniel A. White
  • 174,715
  • 42
  • 343
  • 413
maracuja-juice
  • 826
  • 2
  • 14
  • 33
  • does `content` look ok? – Daniel A. White Feb 14 '18 at 15:48
  • Your `memoryStream` must start with a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark). To handle the BOM use a `StreamReader` as shown in [How do I ignore the UTF-8 Byte Order Marker in String comparisons?](https://stackoverflow.com/a/2915239/3744182) instead of `Encoding.UTF8.GetString()`. In fact I think this is a duplicate. If you agree I'll close it as such. – dbc Feb 14 '18 at 19:51
  • Why the downvote? – maracuja-juice Feb 15 '18 at 12:25

1 Answers1

1

Through a HEX Editor I checked the beginning of the file and I found out that my IDE added a BOM to the beginning of the file. So it looks like this:

[.. {.. "

As they are not recommended I chose to get rid of them by creating a new file with Explorer/Finder and adding my JSON in there.

maracuja-juice
  • 826
  • 2
  • 14
  • 33