-1

I have validated it with http://jsonlint.com/, and it is a valid Json string. However, I cannot parse this with Newtonsoft Json in CSharp. I am using Newtonsoft V9.0.1.

[{
    "Test": {
        "ID": "Test1",
        "Name": "Name1"
    }
}, {
    "Test": {
        "ID": "Test2",
        "Name": "Name2"
    }
}]

The exception returned is: "Unexpected character encountered while parsing value: . Path '', line 0, position 0."

s k
  • 4,342
  • 3
  • 42
  • 61
  • 2
    *I cannot parse this with Newtonsoft Json in CSharp*. What have you tried that did not work? Can you give a [mcve] for your problem? – dbc Jun 30 '16 at 08:44
  • 2
    What do you mean you cannot parse this? Could you please provide more details, and maybe also some code? – Paolo Tedesco Jun 30 '16 at 08:44
  • The code is here, if you insist JsonConvert.DeserializeObject(".... the string goes here..."); – s k Jun 30 '16 at 08:46
  • 1
    I cannot reproduce your problem. See https://dotnetfiddle.net/0jkDO6 – dbc Jun 30 '16 at 08:55
  • How are you getting the string and where do you deserialise it? This could be the problem. As others have said show the code. – TheLethalCoder Jun 30 '16 at 08:56
  • dbc, thanks for taking up the effort to try it out. I edited your sample, and I noticed that the json string sent to me was a UTF-8 string with BOM at the front. Seems like Newtonsoft is not able to handle UTF-8 string with BOM. – s k Jun 30 '16 at 09:01
  • Oh, your JSON string probably has a [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark) hidden at the beginning. To remove see [json.net deserialization throwing exception when json is loaded from project resource](https://stackoverflow.com/questions/32881804) or [Strip Byte Order Mark from string in C#](https://stackoverflow.com/questions/1317700) -- and I see our comments crossed. – dbc Jun 30 '16 at 09:02

1 Answers1

-1

First add the Newtonsoft json using Nuget Package Manager

public class Wrapper
{
    public List<Test> test { get; set; }
}
class Test
{
    public string ID;
    public string Name;
}

You can implement a class that holds the fields you have in your JSON

and then use the generic version of DeserializeObject:

List<Wrapper> tmp = JsonConvert.DeserializeObject<List<Wrapper>(jsonstring);
Ravi Kanth
  • 1,182
  • 13
  • 38