Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2477
  • Last Modified:

How do I validate JSON using C#/Xunit?

After spending two days of research on the simple validation of XML in Xunit (since noone was willing to help with my previous XML validating question), I've finally by myself written a very simple XML syntax validation code in C#. (code SIMPLIFIED EVEN FURTHER below)

Now I am faced with the same problem but this time is validation of JSON.

Please consider the code below and help me with write a simple and straight-forward solution just like the XML validation.

THANKS.


PS, I apologize if I sound a tad rude in my question, but I am losing patient with this PAID website.
using System;
using Xunit;
using System.Xml;
 
public class XMLisValid 
{
    [Fact]
    public void testXMLValid(string source) 
    {
        XmlDocument doc = new XmlDocument();
 
       Assert.DoesNotThrow(
                delegate
                {
                    doc.Load(source);
                });   
    }
}

Open in new window

0
dy211
Asked:
dy211
2 Solutions
 
MogalManicCommented:
To validate the JSON string you need to parse the JSON format and retrieve the elements.  There is a JSON parser here:
  http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
and you can parse it like this article does:
  http://www.jamiedigi.com/2009/06/parsing-twitter-json-in-c-sharp/

So your test code would look something like this:


    [Fact]
    public void testJSONValid(string source) 
    {
       HashTable jsonHash = (Hashtable)Procurios.Public.JSON.JsonDecode(jsonCode);
 
       ArrayList jsonResults = (ArrayList)jsonHash["results"];
       foreach (object objResult in jsonResults) {
          //Add asserts here asserting that jsonResults contains correct values(i.e.  jsonResult["ExpectedVariableName"]  should contain ???)
       }
    }

Open in new window

0
 
dy211Author Commented:
solved this question myself by running a script to run http://www.raboof.com/projects/jsonchecker/
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now