4

I have JSON data:

var decodedJson =
"{{
  "user": {
    "userId": "sid:C4F4E93856104F078A11FE95892F0158"
  },
  "authenticationToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmdWxscm93IjoiYWxsIiwiRGJnMiI6ImxvZ2luIiwidmVyIjoiMyIsInVpZCI6InNpZDpDNEY0RTkzODU2MTA0RjA3OEExMUZFOTU4OTJGMDE1OCIsImlzcyI6InVybjptaWNyb3NvZnQ6d2luZG93cy1henVyZTp6dW1vIiwiYXVkIjoidXJuOm1pY3Jvc29mdDp3aW5kb3dzLWF6dXJlOnp1bW8iLCJleHAiOjE0NDk3NTYzNzIsIm5iZiI6MTQ0NzE2NDM3Mn0.kc-0O_aorfTw9l9U6yY6wyVtQnckqNBJikBzxAcJZ_U"
}}";

Then I want to deserialize it dynamically using JSON.NET:

var result = JsonConvert.DeserializeObject<dynamic>(decodedJson);

Then I expect to extract the UserId and Token like this:

string userId = result.user.userId;
string userToken = result.authenticationToken;

But it is saying

"Unknown member user/ authenticationToken"

Any ideas?


UPDATED:

I have copied wrong json data, it actually should be like this:

{\"user\":{\"userId\":\"sid:C4F4E93856104F078A11FE95892F0158\"},\"authenticationToken\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmdWxscm93IjoiYWxsIiwiRGJnMiI6ImxvZ2luIiwidmVyIjoiMyIsInVpZCI6InNpZDpDNEY0RTkzODU2MTA0RjA3OEExMUZFOTU4OTJGMDE1OCIsImlzcyI6InVybjptaWNyb3NvZnQ6d2luZG93cy1henVyZTp6dW1vIiwiYXVkIjoidXJuOm1pY3Jvc29mdDp3aW5kb3dzLWF6dXJlOnp1bW8iLCJleHAiOjE0NDk3NjE1NDEsIm5iZiI6MTQ0NzE2OTU0MX0.oVH8R2134UQQDpXfzPv2mmrj7M05w2mzWtbp70i7GEU\"}
4
  • Since you know the JSON structure, can't your serialize to a standard type? Commented Nov 10, 2015 at 14:16
  • @ArghyaC: The jSon data may change in the future. Anyway I have followed this code sample from Microsoft Azure API azure.microsoft.com/en-gb/documentation/articles/… Commented Nov 10, 2015 at 14:19
  • 1
    Have you tried removing the opening { and closing }, you have 2 on start and on end and that's illegal in JSON. You can also try this stackoverflow.com/a/8972079/2096538 Commented Nov 10, 2015 at 14:24
  • Sorry I have copied wrong json data, just updated the question Commented Nov 10, 2015 at 15:35

3 Answers 3

2

In the long run you would be better to copy the structure in the C# exactly. If your model changes for json then you will have to change your dynamic code anyway and the bugs are easier to creep in.

The following classes can be used to parse your json after removing the extra {}at the start and end of the response.

public class User
{
    public string userId { get; set; }
}

public class RootObject
{
    public User user { get; set; }
    public string authenticationToken { get; set; }
}

You can utilise the following site to quickly map JSON to CSharp

http://json2csharp.com/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I know it but I just follow the instruction from Microsoft example page and want to know what wrong with it: azure.microsoft.com/en-gb/documentation/articles/…
1

It looks like you have an extra set of curly braces "{}" around the entire object in the JSON string. Have you tried removing those to see if it works?

I pasted your JSON string in a few web-based JSON formatters and they all threw errors until I removed the outermost curly braces.

2 Comments

To the OP - if you receive double braces, you can remove them using : jsonString = jsonString.Substring(1, jsonString.Length - 2);
In some cases valid json when parsed with JSON.Net, the extra curly brackets are added for some reason?? That's what I am stuck on.
1

One way, is to use Linq-to-JSON of Json.NET like this

var jsonString = File.ReadAllText(@"C:\YourDirectory\user.json");
var jObjet = JObject.Parse(jsonString);

string userId = (string)jObjet["user"]["userId"];
string userToken = (string)jObjet["authenticationToken"];

If your object structure changes in future, you can pass the new string tag names as parameter to your method.

Note Your JSON structure is not correct, you have an extra set of braces {} around the object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.