Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading JSON files with C# and JSON.net

Tags:

json

c#

json.net

Im having some trouble to understand how to use JSON.net to read a json file.

The file is looking like this:

"version": {   
    "files": [
        {
            "url": "http://www.url.com/",
            "name": "someName"
        },
        { 
            "name": "someOtherName"
            "url": "http://www.url.com/"
            "clientreq": true
        }, ....

I really do not have much idea how i can read this file .. What i need to do is to read the lines and download the file via the "url".. I know how to download files and so on, but i dont know how i can use JSON.net to read the json file and loop through each section, and download the file..

Can you assist ?

like image 805
Daniel Jørgensen Avatar asked Oct 25 '13 17:10

Daniel Jørgensen


People also ask

Can you use JSON in C?

Parsing JSON in C using microjson Developed originally for server-browser communication, the use of JSON has since expanded into a universal data interchange format. This tutorial will provide a simple introduction to parsing JSON strings in the C programming language using the microjson library.

How can I read JSON files?

JSON files are human-readable means the user can read them easily. These files can be opened in any simple text editor like Notepad, which is easy to use. Almost every programming language supports JSON format because they have libraries and functions to read/write JSON structures.

Can C++ read JSON files?

There's no native support for JSON in C++ but there are a number of libraries that provide support for working with JSON. Perhaps the most widely used is JsonCpp, available from GitHub at https://github.com/open-source-parsers/jsoncpp.


1 Answers

The easiest way is to deserialize your json into a dynamic object like this

Then you can access its properties an loop for getting the urls

dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);

var urls = new List<string>();

foreach(var file in result.version.files)
{
    urls.Add(file.url); 
}
like image 60
Esteban Elverdin Avatar answered Sep 30 '22 18:09

Esteban Elverdin