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.

5
  • this question has already been answered here: stackoverflow.com/questions/11835593/… Commented Apr 16, 2014 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. Commented Apr 16, 2014 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". Commented Apr 16, 2014 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. Commented Apr 16, 2014 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. Commented Apr 16, 2014 at 14:26

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

0

A working code snippet

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

Source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.