简体   繁体   中英

Return JSON from API method

I am using ASP.net Core web api (c#) here

I have a JSON string as:

{
   "userId":321,   
   "account":"new  
   "fname":"Adam",
   "lname":"Silver" 
   "features":[
      {
         "available":true,
         "status":open,
         "admin":false        
      }
   ]
}

I want to test this data in my angular code so wanted to hardcode this into my API; then I want my API to return this back. What I am finding it hard is how to return this. Shall I return this as a string or need to parse it?

I have this method in my API:

[HttpGet]        
public ActionResult<IEnumerable<string>> Get()
{
     return new string[] { "value1", "value2" };
}

Do I need to represent this into string or parse it someway?

Hi Please find correct JSON format for above one:

{
    "userId": 321,
    "account": "new",
    "fname": "Adam",
    "lname": "Silver",
    "features": [{
        "available": true,
        "status": "open",
        "admin": false
    }]
}

you can use below class in your web API to pass respective data

public class Feature
{
    public bool available { get; set; }
    public string status { get; set; }
    public bool admin { get; set; }
}

public class RootObject
{
    public int userId { get; set; }
    public string account { get; set; }
    public string fname { get; set; }
    public string lname { get; set; }
    public List<Feature> features { get; set; }
}

then at the end, while returning data, convert the respective class object into JSON by serializing that into JSON format.

Hope it will fulfill your requirement.

Your JSON is invalid. We need to correct it. JSONLint can be helpful for that. I took your JSON and corrected the syntax errors until I got this:

{
   "userId": 321,   
   "account": "new",
   "fname": "Adam",
   "lname": "Silver",
   "features":[
      {
         "available": true,
         "status": "open",
         "admin": false        
      }
   ]
}

Then I need to generate a C# class structure to represent this JSON. I could manually create it, but the excellent json2csharp.com can generate it for me quickly. I fed this JSON into and received the following classes back:

public class Feature
{
    public bool available { get; set; }
    public string status { get; set; }
    public bool admin { get; set; }
}

public class RootObject
{
    public int userId { get; set; }
    public string account { get; set; }
    public string fname { get; set; }
    public string lname { get; set; }
    public List<Feature> features { get; set; }
}

I put these class definitions into my application. Then I need to modify my action method to create an instance of this RootObject class (you should change the name to actually match what it's intended for).

[HttpGet]        
public ActionResult<RootObject> Get()
{
    // Create an instance of our RootObject and set the properties
    var myRootObject = new RootObject();
    myRootObject.userId = 321;
    myRootObject.account = "new";
    myRootObject.fname = "Adam";
    myRootObject.lname = "Silver";
    myRootObject.features = new List<Feature>();

    // Create an instance of a feature and set its properties
    var feature = new Feature();
    feature.available = true;
    feature.status = "open";
    feature.admin = false;

    // Add the new feature to the features collection of our RootObject    
    myRootObject.features.Add(feature);

    // Return the instance of our RootObject
    // The framework will handle serializing it to JSON for us
    return myRootObject;
}

Note that I changed the signature of your method. I made it no longer accept an IEnumerable because it wasn't clear why you had that. And I changed it to return an ActionResult after checking Microsoft's documentation .

Putting the comments into an answer: If you are using ActionResult, I'll assume you are using asp.net mvc. What you want is JsonResult.

    [HttpGet]
    public JsonResult Get()
    {
        return new JsonResult
        {
            Data = new
            {
                userId = 321,
                account = new
                {
                    fname = "Adam",
                    lname = "Silver",
                    features = new object[]{
                    new
                {
                    available = true,
                    status = "open",
                    admin = false
                }
                }
                }
            },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

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