sergio
sergio

Reputation: 1046

Correct Format for JSON (Json.NET

am trying to consume json strings inside asp.net and google told me about this Json.NET library.

I have the following json from a php webservice:

"[{\"AccountID\":\"demo\",\"UserID\":\"1\",\"Password\":\"*blank*\",\"Active\":\"1\",\"Name\":\"\"}][{\"AccountID\":\"demo\",\"UserID\":\"1\",\"Password\":\"*blank*\",\"Active\":\"1\",\"Name\":\"\"}]"

i found that the library cannot load urls so i had to use System.Net.WebClient; ok so far. The problem is that doing

var json = webClient.DownloadString(url); //gives the json above               
object user = JsonConvert.DeserializeObject(json);
// or User user =JsonConvert.DeserializeObject<User>(json); wont work

wont work the library says "After parsing a value an unexpected character was encountered"

So, is my json malformed? i construct it with json_encode($resultset) from php, so i wonder whats going on.

my User object only has the properties i have on json.

Upvotes: 2

Views: 2506

Answers (2)

Myra
Myra

Reputation: 3656

That's because your json string is in incorrect format
If you're going to use brackets [ then json has only one for arrays.Since your declaration is likely an array.Put your string(as I edited) into this site and see the result yourself.

[{"AccountID":"demo","UserID":"1","Password":"*blank*","Active":"1","Name":""},{"AccountID":"demo","UserID":"1","Password":"*blank*","Active":"1","Name":""}]

Upvotes: 0

Codo
Codo

Reputation: 78825

First of all, your JSON string has double quotes around the whole result and a backslash before every other double quote. I'll assume that this is an artifact from copying it out of the VisualStudio debugger (which displays it like this).

If we remove these backslashes, we get the following JSON:

[{"AccountID":"demo","UserID":"1","Password":"*blank*","Active":"1","Name":""}]
[{"AccountID":"demo","UserID":"1","Password":"*blank*","Active":"1","Name":""}]

These are two identical JSON arrays. But a valid JSON response consists of either a single JSON object or a single JSON array. This response is invalid.

It's seems that two JSON response where concatenated, resulting in an invalid construct.

Upvotes: 2

Related Questions