1

I have some JSON which is valid but a little quirky looking. Here it is:

{
"server":"some server name",
"files":
{
"filename1":{"source":"original","format":"text"},
"filename2":{"source":"original","format":"text"},
"filename3":{"source":"original","format":"text"}
}
}

As you can see, the "files" section contains one JSON object per "file" so I can get this as array of JTokens but not sure how to get the values of "filename1", "filename2", etc. out.

I'm using JSON.NET and C# so please don't provide an answer that requires the JavaScriptSerializer from System.Web.Extensions.dll. Either pure JObject/JToken calls or JConvert.DeserializeObject<> would be okay.

Thanks.

4
  • 1
    Are you stuck with that format? Commented Feb 5, 2013 at 19:47
  • Yes it's a format coming from the server and can't be changed. Commented Feb 5, 2013 at 19:56
  • Why don't you combine the attributes to {server:"name", files :{filename:, "file1",format:" text", source:"original"}}} Commented Feb 5, 2013 at 20:03
  • @lafama If I could change the JSON I would but that's not the problem. The problem is given this JSON output how would I get the filename values? Commented Feb 5, 2013 at 20:06

3 Answers 3

2

Try this

public class Data
{
    public Data()
    {
        Files = new Dictionary<string, FileData>();
    }
    public string Server { get; set; }
    public IDictionary<string, FileData> Files { get; set; } 
}
public class FileData
{
    public string Source { get; set; }
    public string Format { get; set; }
}

Then Access it using this

var result = JsonConvert.DeserializeObject<Data>(JsonValue);
Sign up to request clarification or add additional context in comments.

1 Comment

should note that the values the OP wants will be enumerable from result.Files.Keys
2

How about using dynamic deserialization? See Deserialize json object into dynamic object using Json.net

string json = @"{""server"":""some server name"",""files"":{""filename1"":{""source"":""original"",""format"":""text""},""filename2"":{""source"":""original"",""format"":""text""},""filename3"":{""source"":""original"",""format"":""text""}}}";
dynamic result = JObject.Parse(json);

Console.WriteLine(result.server);
foreach (dynamic file in result.files)
{
    Console.WriteLine(file.Name);
    dynamic value = file.Value;
    Console.WriteLine(value.source);
    Console.WriteLine(value.format);
}

Output

some server name
filename1
original
text
filename2
original
text
filename3
original
text

Comments

1

You must define a class like this:

class ClassName
{
    public string server;
    public ClassName2 files;
}

and define ClassName2:

class ClassName2
{
    ClassName3 filename1;
    ClassName3 filename2;
    ClassName3 filename3;
}

and finally ClassName3

ClassName3
{
    public string source;
    public string format;
}

supporse you have saved your json data in a string variable like 'result'

 ClassName fin = JsonConvert.DeserializeObject<ClassName>(result);

this will give you anything you need.

3 Comments

This is good and would work however one catch. The filename is different in the dataset. So while this works in the snippet I posted, it doesn't work when there's "filename10" or "filename_123.zip". The names are dynamic and determined at runtime and will be different with each call so need something that can handle that.
if your filenames are dynamic it is better to define them as array.
Agreed, just looking how to do that (although I'm working through the answer @lafama proposed below and it looks like it will work)

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.