2

Wrote a simple function for Web API 2 which returns the list of countries. It returns the valid Json format but without the array/object name. I have a bit of difficulty understand how this is achieved?

Here is my C# code:

[Route("Constants/CountryList")]
[HttpGet]
public IHttpActionResult GetCountryList()
{
    IEnumerable<ISimpleListEntity> list = new CountryStore().SimpleSortedListByName();
    if (list == null || !list.Any())
    {
        return NotFound();
    }

    return Ok(list);
}

ISimpleListEntity interface code is here.

public interface ISimpleListEntity
{
    int Id { get; set; }
    string Name { get; set; }
}

This service returns the following Json output (without the object/array name):

[  
   {  
      "Id":1,
      "Name":"[Select]"
   },
   {  
      "Id":4,
      "Name":"India"
   },
   {  
      "Id":3,
      "Name":"Singapore"
   },
   {  
      "Id":2,
      "Name":"United Arab Emirates"
   }
]

But, am struggling to achieve the following Json format (WITH the object/array name called 'CountryList'):

{  
   "CountryList":[  
      {  
         "Id":1,
         "Name":"[Select]"
      },
      {  
         "Id":4,
         "Name":"India"
      },
      {  
         "Id":3,
         "Name":"Singapore"
      },
      {  
         "Id":2,
         "Name":"United Arab Emirates"
      }
   ]
}

4 Answers 4

9

You could either create a specific class for this, as per the answer from Boas, or just use an anonymous type:

return Ok(new { CountryList = list });

Basically, you need an object with the appropriate property, one way or the other. If you want to deserialize this later and keep compile-time checking, it would be worth creating a class - but if you're either using dynamic typing or the consumer won't be the C# code anyway, then an anonymous type would be simpler.

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

3 Comments

@MohitShrivastava: I have no idea what you mean, I'm afraid. The OP is producing JSON, not consuming it.
That works like a charm! :) Thanks! Do I need to worry about performance issue because we are creating a new object?
@CoderAbsolute: No - it's one object, and you're responding to a network request. You're already doing work to get the data... creating a single extra object is going to be utterly insignificant.
4

thats because you are serializing a list

Yu could create a dto which has the property with the name you want and serialize this in stead of the list

public class MyDto
{
      public List<ISimpleListEntity> CountryList {get;Set;}
}

3 Comments

Just curious, how do I make use of this class in my service? Sorry for the stupid question, please understand am a beginner!
just create it var instance = new MyDto(){CountryList = list} Also have a look at @jon skeets reply he has posted a more elegant way
Infact, I have already implemented Jon Skeet's suggestion and it works for me. Then, I saw your proposal and started to wonder...
1

You can simply use an anonymous type:

return Ok(new {
    CountryList = list
});

3 Comments

Isn't this the same as Jon Skeet's answer to this question?
@WaiHaLee I see it's the same answer. When I answered it was'nt still present, see the answer time, anyway I received a downvote I think for that reason.
Ah - you're right. Jon beat you by 1 minute and 4 seconds - presumably while you were writing your answer he posted his.
0

If you want to return the object name too then it'd be better to return a model that contains that list.

class Model
{
    IEnumerable<ISimpleListEntity> CountryList { get; set; };
}

Then in your controller

return Ok(new model() {CountryList= ... }); 

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.