Search code examples
c#jsonparsingsystem.json

JSON parse exception using System.Json


I get an error while parsing a json string into an object. I am using system.json to parse the json string.

The JSON file: (NOTE: I cannot change the structure of this json file because it is generated)

{
    title: "My Title",
    log: "",                    
    nid: "1234",
    type: "software",
    language: "EN",
    created: "1364480345",
    revision_timestamp: "1366803957",
    body: {                 
         und: [
              {
                  value: "abc",
                  summary: "def"
              }
         ]
    }
}

The C# code:

string jsonString = new WebClient().DownloadString(".......MyJson.json");  //For test purpose

var obj = JsonObject.Parse (jsonString);  ///<--- At this line the exception is thrown 

The Exception:

System.ArgumentException has been thrown.
Invalid JSON string literal format. At line 1, column 2

How to solve this?

Thanks in advance!


Solution

  • How to solve this?

    (NOTE: I cannot change the structure of this json file because it is generated)

    Easy, use json.Net. it works without any problem with your json

    var j = JObject.Parse(jsonString);
    

    You can even use dynamic keyword

    dynamic j = JObject.Parse(jsonString);
    
    Console.WriteLine("{0},{1}", j.title, j.body.und[0].value);