2

I'm hitting a RESTful api and trying to process the returned data. The data itself consists of json arrays within arrays.

{
  "category1": [
    [
      "http://url1",
      "URL 1 Title"
    ],
    [
      "http://url2",
      "URL 2 Title"
    ]
  ],
  "category2": [
    [
      "http://url3",
      "URL 3 Title"
    ]
  ],
  "category3": [
    [
      "http://url4",
      "URL 4 Title"
    ]
  ]
}

I've created a class that maps to the content above, but I have no idea of how to parse the result and extract the individual URL and URL Titles.

I can see how I might do it if these were objects, but no idea on how to access directly nested arrays.

4
  • You could do: var objectOfYourClass = JsonConvert.DeserializeObject<YourClass>(jsonString) Commented Mar 24, 2017 at 17:31
  • The result of that JSON will be an object with three properties and not an array with three objects. Commented Mar 24, 2017 at 17:31
  • please add your current code Commented Mar 24, 2017 at 17:41
  • Please find my sample code here: dotnetfiddle.net/KM66mQ Commented Mar 24, 2017 at 18:09

3 Answers 3

1

Try this

class Output
{
     public string[][] category1 { get; set; }
     public string[][] category2 { get; set; }
     public string[][] category3 { get; set; }
}

class OutputCategory
{
     public string uri { get; set; }
     public string label { get; set; }
}

I have modified your code in the main() method to the following one.

 string samplejson =
            @"{'category1':[['http://url1','URL 1 Title'],['http://url2','URL 2 Title']],'category2':[['http://url3','URL 3 Title']],'category3':[['http://url4','URL 4 Title']]}";

        //deserialised to my class object
        var result = JsonConvert.DeserializeObject<Output>(samplejson);

        //create new output object
        Output outCat = new Output();

        //I now want to assign the appropriate values to my new output object

        Console.WriteLine(result.category2.Length);
        Console.WriteLine("URL: " + result.category1[0][0]);
        Console.WriteLine("Title: " + result.category1[0][1]);

Make sure you check the length of the array and then proceed with your result.

Sign up to request clarification or add additional context in comments.

2 Comments

Many Thanks for the help!
+1 for a good example of how to access a more deeply nested array. Most of the examples start with an array or an object and leave it at that.
0

I am using this in my codes, after you have YourModel class created then you can use the bellow approach

 JsonConvert.DeserializeObject<YourModel>(content,
                                        new JsonSerializerSettings
                                        {
                                           ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                                        });

Comments

0

You want to have jagged arrays:

public class Model
{
    public string[][] category1 { get; set; }
    public string[][] category2 { get; set; }
    public string[][] category3 { get; set; }
}

And then:

var model = JsonConvert.DeserializeObject<Model>(json);

foreach (var element in model.category1)
{
    var url = element[0];
    var title = element[1];
}

Comments

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.