Solved

How do I validate JSON using C#/Xunit?

Posted on 2009-06-29
2
2,372 Views
Last Modified: 2013-11-08
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
Comment
Question by:dy211
2 Comments
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 100 total points
ID: 24744057
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
 

Accepted Solution

by:
dy211 earned 0 total points
ID: 24817586
solved this question myself by running a script to run http://www.raboof.com/projects/jsonchecker/
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question