2

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.

David Fox
  • 10,603
  • 9
  • 50
  • 80
  • this question has already been answered here: http://stackoverflow.com/questions/11835593/validate-a-string-to-be-json-or-not-in-asp-net – Andrew Apr 16 '14 at 13:52
  • What is it you want to achieve, exactly? Why would you remove the commas? Keep in mind that C#, by itself, has no active knowledge of JSON. JavaScriptSerializer is an option though. – Flater Apr 16 '14 at 13:52
  • @Flater sir basically I want to restrict the webservice that its give response if the input string is in valid json format other wise its return message "json is not in the correct format". – Fahad Tahir Apr 16 '14 at 14:14
  • @Andrew I implement the code which you stated it is working fine but if the , (coma) is missing in the json string so for that what is the procedure. – Fahad Tahir Apr 16 '14 at 14:17
  • @FahadTahir: What are you using the JSON for? If it will be functionally used in your code, that component will start complaining if the JSON string you supplied it is invalid. If it's an exception, you can catch it and send your error message back to the client. If it's used only as a string to be sent to the browser, then it's the browser (read: your javascript) who needs to handle invalid JSON. In my opinion, there's no point in having a backend validate something if it will not be functionally used in that backend. – Flater Apr 16 '14 at 14:26

4 Answers4

1

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

hman
  • 486
  • 4
  • 7
0

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

Hogan
  • 69,564
  • 10
  • 76
  • 117
0

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

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
0

A working code snippet

public bool isValidJSON(String json) {
try {
JToken token = JObject.Parse(json);
return true; 
}
catch(Exception ex){ 
return false;
} 
}

Source

Community
  • 1
  • 1
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241