简体   繁体   中英

Add arrays to a higher dimension array

I am creating an app that gets information and download movies from a site. I have succeeded in setting the information in different arrays and combining them together to 1 array.

However, now I am trying to put that array inside a bigger array (want to work with JSON), and that is failing.

My current Json output looks like this:

 {"moviedata": 
    ["http:/xxxx","title title ","k0X0HrYtMm5u"],"tags": 
    ["x","x","x","Prop","x","x","x","x","x","x","x"],"categorydata": 
    ["x","x","x"]}

Which is achieved with this code:

Dictionary<string, string[]> movie = new Dictionary<string, string[]>();

moviedata.Add("moviedata", moviegenerala);
moviedata.Add("tags", tagsa);
moviedata.Add("categorydata", categoriesa);
string moviejson = JsonConvert.SerializeObject(moviedata);

I want to add another layer on top of this, so there is a general tab named "Movie", and for each movie it has this information. How would I do that?

UPDATE: Added this code:

var myShape = new
    {
        Movie = new Dictionary<string, string[]>
        {
            ["moviedata"] = moviegenerala,
            ["tags"] = tagsa,
            ["categorydata"] = categoriesa,
            ["localdata"] = localdataa
        }
    };
    var moviejson = JsonConvert.SerializeObject(myShape);

The JSON seems to be correct, but it cannot parse, after the first listing it says there is text after the closing bracket.

UPDATE: Changed code to this:

Dictionary<string, string[]> movies = new Dictionary<string, string[]>();
                                var myShape = movies.Select(m => new
                                {
                                    Movie = new Dictionary<string, string[]>
                                    {
                                        // TODO - you'll need to get the appropriate data for this movie
                                        ["moviedata"] = moviegenerala,
                                        ["tags"] = tagsa,
                                        ["categorydata"] = categoriesa,
                                        ["localdata"] = localdataa
                                    }

                                });

                                var moviejson = JsonConvert.SerializeObject(myShape);
                                File.AppendAllText("moviedata.txt", moviejson);

But now it only gives empty brackets :(

If this is a one-off serialization, then simplest could be just to wrap the Dictionary with an anonymous class adding the extra property indentation, eg

var myShape = new 
{
     Movie = new Dictionary<string, string[]>
     {
         ["moviedata"] = moviegenerala,
         ["tags"] = tagsa,
         ["categorydata"] = categoriesa
     }
};
var moviejson = JsonConvert.SerializeObject(myShape);

However, if this same shape is required in multiple places in your code, then I would recommend that you create a fully fledged class for your 'Shape'.

Edit:

The json produced by the above is

{
  "Movie": {
    "moviedata": [ "http:/xxxx", "title title ", "k0X0HrYtMm5u" ],
    "tags": [ "x", "x", "x", "Prop", "x", "x", "x", "x", "x", "x", "x" ],
    "categorydata": [ "x", "x", "x" ]
  }
}

If you need to map multiple Movies, then project then you can use a .Select :

// TODO : Replace this with your actual current movies
var inMovies = new[] {new Movie{...},new Movie {...}, ...};
var myShape = inMovies.Select(m => new
{
    Movie = new Dictionary<string, string[]>
    {
        // TODO - you'll need to get the appropriate data for this movie, e.g. m.moviegenerala
        ["moviedata"] = moviegenerala, 
        ...
    }
});

Produces the following:

[
  {
    "Movie": {
      "moviedata": [ "http:/xxxx", "title title ", "k0X0HrYtMm5u" ],
      "tags": [ "x", "x", "x", "Prop", "x", "x", "x", "x", "x", "x", "x" ],
      "categorydata": [ "x", "x", "x" ]
    }
  },
  {
    "Movie": {
      "moviedata": [ "http:/xxxx", "title title ", "k0X0HrYtMm5u" ],
      "tags": [ "x", "x", "x", "Prop", "x", "x", "x", "x", "x", "x", "x" ],
      "categorydata": [ "x", "x", "x" ]
    }
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM