6

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 ?

2 Answers 2

9

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); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I ended up using this method as it looked the most simple which seems to work! Thanks a bunch. For info i used streamreader to read the contents of the file with readtoend, which i then used as the "jsonString"
6

http://json2csharp.com/ helps you create C# classes based on your JSON data type. Once you have your classes to match your data, you can deserialize with Json.NET and then work with your data:

var myMessage = JsonConvert.DeserializeObject<MyMessage>(myString);
foreach (var file in myMessage.Version.Files)
{
    // download file.Url
}

Or you can access it as a dynamic object:

dynamic myMessage = JsonConvert.DeserializeObject(myString);
foreach (var file in myMessage.version.files)
{
    // download file.url
}

If you use classes, they might be:

public class File
{
    public Uri Url { get; set; }
    public string Name { get; set; }
    public bool? ClientReq { get; set; }
}

public class Version
{
    public IList<File> Files { get; set; }
}

public class MyMessage
{
    public Version Version { get; set; }
}

(note that Json.Net is smart enough to map properties where the case is different, and turn the URLs into Uri objects) It works when the string is like:

string myString = @"{""version"": {   
    ""files"": [
        {
            ""url"": ""http://www.url.com/"",
            ""name"": ""someName""
        },
        { 
            ""name"": ""someOtherName"",
            ""url"": ""http://www.url.com/"",
            ""clientreq"": true
        }]}}";

4 Comments

"note that Json.Net is smart enough to map properties where the case is different" that's not exactly true, you need to use a CamelCasePropertyNamesContractResolver for that.
@EstebanElverdin it seems to do that by default, then, because the code I've got works, (which maps clientreq to ClientReq) without explicitly specifying any contract resolver.
yes, you are right, no need to do it in your example, I will review some code I was working and see why it does not did it by default.
Thanks for the link to json2csharp.com I can see myself using that a lot. Would have taken me ages to work the structure out by myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.