Validate JSON and XML? FROM#

Im using newtonsoft json.net http://json.codeplex.com/ and I would like to know ...

how to check json and xml are valid json / xml.

how can i check this?

+6
json c # xml
Jan 07 2018-12-12T00:
source share
2 answers

Where you want to check json, server side or client side. Assuming you want to do this on the server side, try to deserialize the json string. if it breaks then it is not valid json. Use JavaScriptSerializer for Deserialization Purpose

var serializer = new JavaScriptSerializer(); var result = serializer.Deserialize<Dictionary<string, object>>(json); 
+7
Jan 07 '12 at 3:11
source share
— -

If you use JSON.net software, you can do the same as Anand. Just deserialize the JSON string, and if it breaks or fails, then this is not a valid JSON structure. Now, if you are trying to do something like http://jsonlint.com/ , then you are probably going beyond what we could help the forums with. If you want to check for errors or not, just use the following C # code, where the result is a JSON string:

 var root = JsonConvert.DeserializeObject<RootObject>(result); 

where the information you want to deserialize from the JSON string should have a RootObject class that looks like:

 public class RootObject { // You would need to create items here to store each of the objects' information in the JSON file. // For example: public string itemName { get; set; } public int itemID { get; set; } } 

Now this assumes that you know the information that MUST be in the JSON file. Otherwise, that the whole program is on its own.

+1
Jan 07 2018-12-12T00:
source share



All Articles