Question

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!

Was it helpful?

Solution 2

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);

OTHER TIPS

You can't. That isn't valid json. Field names must be enclosed in quotes. All json parsing tools will throw when trying to parse that.

You could process it and turn it to valid json before deserializing, but really, you need to correct it API side. No clients will work with that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top