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\"}
Minh Nguyen
  • 2,106
  • 1
  • 28
  • 34
  • Since you know the JSON structure, can't your serialize to a standard type? – Arghya C Nov 10 '15 at 14:16
  • @ArghyaC: The jSon data may change in the future. Anyway I have followed this code sample from Microsoft Azure API https://azure.microsoft.com/en-gb/documentation/articles/app-service-api-dotnet-consume/ – Minh Nguyen Nov 10 '15 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 http://stackoverflow.com/a/8972079/2096538 – Daniel Wardin Nov 10 '15 at 14:24
  • Sorry I have copied wrong json data, just updated the question – Minh Nguyen Nov 10 '15 at 15:35

3 Answers3

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/

James Moore
  • 127
  • 6
  • Thanks. I know it but I just follow the instruction from Microsoft example page and want to know what wrong with it: https://azure.microsoft.com/en-gb/documentation/articles/app-service-api-dotnet-consume/ – Minh Nguyen Nov 10 '15 at 15:41
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.

Daniel T.
  • 41
  • 7
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.

Arghya C
  • 9,805
  • 2
  • 47
  • 66