Reputation: 21
I have json string which will pass to the webservice to perform some action on it. My json string will b like this example:
{"ID":"2","Name":"Tom","data":"[22.3,23.4,21.5]"}
I want to validate the json string if I remove the , (coma):
{"ID":"2""Name":"Tom""data":"[22.3,23.4,21.5]"}
From the json string so its return error message json is not in the correct format.
Upvotes: 2
Views: 1993
Reputation: 32320
A working code snippet
public bool isValidJSON(String json) {
try {
JToken token = JObject.Parse(json);
return true;
}
catch(Exception ex){
return false;
}
}
Upvotes: 0
Reputation: 12044
Maybe you can try creating the JSON with the function ToJSON()
List<MyObject> people = new List<MyObject>{
new MyObject{ID = 1, Name= "Tom", Data= "[22.3,23.4,21.5]"},
new Person{ID = 2, Name= "Tome", LastName = ""[22.3,23.4,21.5]"}
};
string jsonString = people.ToJSON();
And if you with have the string as JSON you can do something like:
JsonConvert.SerializeObject(jsonString ).Dump();
Or using the Networking JSON: http://james.newtonking.com/json
Upvotes: 0
Reputation: 496
JSON.net and JSONSharp allow you to parse JSON into an object and will have the ability to validate or at least catch an exception on errors
Upvotes: 1
Reputation: 70538
Try
var dynamicObject = Json.Decode(jsonString);
And see if it raises an error.
You may need to install the DLL for this separately. I believe it is in the MVC library download.
http://msdn.microsoft.com/en-us/library/system.web.helpers.json%28v=vs.111%29.aspx
Upvotes: 0