Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while using Newtonsoft.Json to parse a Json string

My JSON string looks like this. Please note that it has escape characters.

string json = "\"{\\\"Status\\\":true,\\\"ID\\\":24501}\"";

When I use the Parse method like below I run into an error stated below:

JObject o = JObject.Parse(json);

Error reading JObject from JsonReader. Current JsonReader item is not an object: String

How do I get rid of this error or is there any other method to parse my json string and fetch the values?

like image 604
SVI Avatar asked Aug 15 '11 21:08

SVI


People also ask

What is JObject parse in C#?

4.8. Jobject. Parse() method is an object class method and this method is used to parse the JSON string into the objects of C#. Based on the key value it parses the data of string and then it retrieves the data by using the key values.

What is JObject and JToken?

The JToken hierarchy looks like this: JToken - abstract base class JContainer - abstract base class of JTokens that can contain other JTokens JArray - represents a JSON array (contains an ordered list of JTokens) JObject - represents a JSON object (contains a collection of JProperties) JProperty - represents a JSON ...

What is Jsonconvert SerializeObject C#?

SerializeObject Method (Object, Type, JsonSerializerSettings) Serializes the specified object to a JSON string using a type, formatting and JsonSerializerSettings. Namespace: Newtonsoft.Json.

Why we use Newtonsoft JSON DLL?

The Newtonsoft. JSON namespace provides classes that are used to implement the core services of the framework. It provides methods for converting between . NET types and JSON types.


2 Answers

Remove first and last quotes:

string json = "{\"Status\":true,\"ID\":24501}";

See the Json format here.

like image 114
MByD Avatar answered Sep 19 '22 01:09

MByD


It seems like your object is double encoded. Try:

string json = "{\"Status\":true,\"ID\":24501}";
like image 27
Joe Avatar answered Sep 21 '22 01:09

Joe