How do I parse a JSON array response in JScript .NET 4.0?

1.2k views Asked by At

I have a question about parsing JSON data with JScript .NET, 4.0. I am using Visual Studio 2005 running in Windows XP to make a Windows form (as opposed to a website).

My RESTful API calls return JSON data in the form of an array of objects, for example:

[{"country": "USA", "state": "CA", "city": "San Francisco”}, {“country”: “USA”, “state”: “WA”, “city”: “Seattle”}, {"country": "USA", "state": “MA", "city": “Boston”}], 

and I don’t know how to parse it into a usable object. I think I need something like a serializer/deserializer, like DataContractJsonSerializer to do this, see MSDN, but am having difficulty implementing it in JScript .NET 4.

2

There are 2 answers

0
o.carltonne On

I'm not sure if you've already moved on from this, and I probably need more information to help, but I have a collection scripts that serve as a library to do common things in the jscript. One common task is I might make call from one of these scripts to some api and get json back. In order to parse it, I went to json.org and downloaded json2.js. Once I got that, I'm able to call:

var data = JSON.parse(string_from_api)

Just as I would in javascript on a modern browser. I included json2.js in the same directory as all my other scripts in the library. The problem is that JScript.net (to my knowledge) doesn't have the JSON object that is in modern browsers. You have to add it yourself. Once you do, you can take advantage of the JSON object serialization and deserialization it offers.

0
Otavio On

This is what I've done!

var cfg = eval('('+File.ReadAllText(Directory.GetCurrentDirectory()+"\\Config.json")+')');

Directory.GetCurrentDirectory() is by path applicacion

File.ReadAllText(...) is by read File json

eval('('+ $$ +')') .. Eval.. XD