How Do I Read An External JSON File Using Json.NET on page load?

I have a text file named gisQuery129.json that is created using Json.NET - http://james.newtonking.com/projects/json/help/

There is an example on how to create a JSON file and I have done that successfully, but I want to read the file on a page load event and add the values to .NET textbox controls for input values in a query.

The created file looks like the following:

{
“myData”: {
“randomNumber”: “129”,
“Application”: “MyMapApp”,
“FeatureId”: “ALL”,
“Feature”: “ALL”,
“spatialQuery”: “FEATURE_ID= ‘11111’ AND REQ_SEQ_NUM= 1 AND RAND_PARAM= 129 OR FEATURE_ID= ‘22222’ AND REQ_SEQ_NUM= 2 AND RAND_PARAM= 129”
}
}

I want to read the data in the above sample JSON data and then populate my textboxes something like this:

txtRandParam.text = “129”
txtApplication.text = “MyMapApp”
and three more text boxes after that.

Any thoughts?

I voted XML. LINQ2XML would make your life easier. Anyone agree? Never used JSON, so I have no opinion.

I have worked a lot with XML, so for me there’s a “comfortable” factor, but I like that JSON is more compact (less characters = less file weight).

I’ve only generated JSON using PHP so I’m not the best to answer any .NET specific details.

And even at that my experience with it is very limited.

I think the RFC example for objects is missing a couple of closing curly braces but what do I know. http://www.ietf.org/rfc/rfc4627.txt

My PHP script returns a JSON formatted array of the content-type application/json

var results = '[{"Id":"52","date":"2010-05-05","initialCount":"10264","finalCount":"69138"}]';

And then I use javascript to deal with it. This example uses document.write() but you should do DOM stuff instead.

<script type='text/javascript'>
	var jsonResponse = eval('(' + results + ')');
</script>
</head><body>
<script type="text/javascript">
if (typeof jsonResponse.error == "undefined")
{
	document.write('<h1>' + jsonResponse.length + ' results</h1>');
	for (var i = 0; i < jsonResponse.length; i++)
	{
		document.write(jsonResponse[i].Id + ' ' + jsonResponse[i].finalCount + '<br />');
	}
}
else
{
	document.write(jsonResponse.error + '<br />');
}
</script>

I’m not too keen on using eval() but AFAIK that’s the only way when using javascript and as long as it’s my data I guess I’m safe enough.

With PHP, I could also call the file output on page load and use PHP’s JSON functions to get at the data. I’m guessing .NET has similar capabilities but I don’t know what they might be.

I like JSON quite a bit more than xml for most things. Mainly because it is alot more readable. XML has one big advantage inasmuch as you can define semantics of your document using schemas and such. But there is no reason why a modern web developer should not be conversant in both technologies.

Anyhow, as for the question, did you look at this page on the site you linked? It is dead easy, just deserialize.

Thanks Mittineague.

I ended up figuring it out for both reading and writing from and to JSON. I wish there was something where I could tag this for later as I would like to share what I came up with once I have it a little cleaner. A tips section would be great too.

I do want to learn PHP as well, so I do look forward to trying your code out.

Thanks again.