Solved

How do I validate JSON using C#/Xunit?

Posted on 2009-06-29
2
2,334 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
Comment Utility
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
Comment Utility
solved this question myself by running a script to run http://www.raboof.com/projects/jsonchecker/
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now