0

I created a Web API and it needs to read the response in header with json content, therefore I use code below:

HttpRequestMessage re = Request;
var payLoadJson = re.Content;
string jsonContent = payLoadJson.ReadAsStringAsync().Result;
var test = JObject.Parse(jsonContent);

And here is the response of var test

{{
  "pushToken": "AAABBBCCC"
}}

Can any body tell me how to get the value of pushToken?

JObject.Parse(jsonContent)['pushToken'] always get null because of double braces.

3
  • You can remove your first and last char from the json string to make it a valid JSON Commented Nov 4, 2016 at 2:39
  • Your JSON is invalid. Commented Nov 4, 2016 at 2:40
  • How to let it to be valid? Remove the braces will error in reading JObject when the string create Commented Nov 4, 2016 at 2:55

1 Answer 1

1

Your string in valid Json format:

{
    "pushToken": "AAABBBCCC"
}

Getting the payload

var pushToken = JObject.Parse(json)["pushToken"];

(Also note that pushtoken is between double quotes not single quotes as per your example, you should get an error with singles once)

or

dynamic obj = JsonConvert.DeserializeObject(json);

var pushToken = obj.pushToken;

As a side note:

In case you receive that bad invalid Json like you described ... I would actually have no idea how that's even possible. Then remove the two braces before parsing or deserializing.

json = json.Trim();
json = json.Substring(1, json.Length - 2);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your detail ans, it is work now! The invalid Json is come from Apple wallet return HTTP content, and I am still finding what Apple return data exactly. Once again, thank you so much!
@YukwongTsang your welcome. But any API should return you valid Json after all.

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.