0

How can I create a JsonArray with a child data object array? I am using Web service and C#.

I want the result of the JsonArray to look like the following:

[{
    "name": "Deadpool",
    "url": {
        "small": "http://api.android.info/images/small/deadpool.jpg",
        "medium": "http://api.android.info/images/medium/deadpool.jpg",
        "large": "http://api.android.info/images/large/deadpool.jpg"
    },
    "time": "February 12, 2016"
},
{
    "name": "The Jungle Book",
    "url": {
        "small": "http://api.android.info/images/small/book.jpg",
        "medium": "http://api.android.info/images/medium/book.jpg",
        "large": "http://api.android.info/images/large/book.jpg"
    },
    "time": "April 15, 2016"
},
{
    "name": "X-Men: Apocalypse",
    "url": {
        "small": "http://api.android.info/images/small/xmen.jpg",
        "medium": "http://api.android.info/images/medium/xmen.jpg",
        "large": "http://api.android.info/images/large/xmen.jpg"
    },
    "time": "May 27, 2016"
}]
4
  • What exactly are you trying to do? You current question is not that clear. Can you add some more details to your questions? Commented Nov 22, 2016 at 4:41
  • How do you access to your data ? from a database ? Do you use linq ? Commented Nov 22, 2016 at 4:50
  • I think what you want is how to create your class structure to convert your data in to a jsonarray like your example. Is this is what you expecting Commented Nov 22, 2016 at 4:57
  • @Seminda yes thats it.. Commented Nov 22, 2016 at 9:12

1 Answer 1

1

First, create the models that can output the given data. You need a MovieModel, a movie can have multiple image sizes and urls stored, we use a dictionary for this.

UPDATED

MovieModel.cs

public class MovieModel
{
    public string Name { get; set; }
    public Dictionary<string,string> Url { get; set; }
    public string Time { get; set; }
}

Now you need to install Newtonsoft.Json from Nuget packages. Then import it.

using Newtonsoft.Json;

Initialize the model and convert to Json using SerializeObject() method.

var movieList = new List<MovieModel>
{ 
    new MovieModel
    {
        MovieName = "Deadpool",
        Time = DateTime.UtcNow.ToString("t"),
        Url = new Dictionary<string, string>
        {
            { "small", "http://api.android.info/images/small/deadpool.jpg" },
            { "medium", "http://api.android.info/images/medium/deadpool.jpg" }
        }
    }
    // .. add more movies .. //
};

// convert to camelcase and set indentation
var output = JsonConvert.SerializeObject(
    movieList,
    Formatting.Indented,
    new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    }
);

// testing output on console
Console.WriteLine(output);

In a real application, you would create Movie instances by getting data from a database, not initializing it for yourself as used in this example.

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

3 Comments

it works mr.abdul, but in result "url" : [{ "small" : "http.....", "medium" : "http....." }] i dont want to put "[" in list data "url"
Why do you want to remove it? Square brackets in Json means it's an array of items, if you remove it, consuming applications might have trouble deserializing back to an object.
because in tutorial json is that, so i want to create like that,, you can check this url : api.androidhive.info/json/glide.json ..

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.