Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Model classes used:

C#
public class Location    
    {
        public string id { get; set; } 
        public int area { get; set; } 
        public int region { get; set; } 
        
    }

public class Root 
    {
        public List<Location> locations { get; set; }
    }


My JSON response is like:

[
    {
        "locations": [ // some data]
    }
]


I do not want [] brackets at start and end of JSON response, it's not an array. I see the same output in browser and postman.

What I have tried:

Controller code for Get() method:

C#
// ControllerObject created here

public HttpResponseMessage Get()
        
        {
            

            HttpResponseMessage response = null;
            
            // call to SP
            
            if (reader.HasRows)
            {
                

                //ClassObj created here

		ClassObj.locations = new List<Location>();

                while (reader.Read())
                    {
                        // data added to list locations here
                        
                    }
                                 
                ControllerObject.Add(ClassObj);
                
               
                
                var jresp = JsonConvert.SerializeObject(ControllerObject, Formatting.Indented);
                
                response = Request.CreateResponse(HttpStatusCode.OK,"Success");
                response.Content = new StringContent(jresp, Encoding.UTF8, "application/json");

                return response;
            }
            else
            { 
                return Request.CreateResponse(HttpStatusCode.NotFound, response);
            }

        }
Posted
Updated 17-Sep-20 0:40am

It's "not an array", but it is a collection:
C#
public class Root 
    {
        public List<Location> locations { get; set; }
    }
Since JSON doesn't have any specific delimiters for .NET collections, it uses the array markers to delimit List<T> and so forth.

If you don't want that, don't use a collection!
 
Share this answer
 
Comments
Member 14938958 17-Sep-20 4:49am    
Thanks,
What should be my class to have output like :
{
"locations": [ // some data]
}
Quote:
var jresp = JsonConvert.SerializeObject(ControllerObject, Formatting.Indented);

Not sure where from (how) you are getting data in json format here: ControllerObject
Now, if you have the control on it, you can avoid passing square brackets as per your need. If not, then see below:

Quote:
response.Content = new StringContent(jresp, Encoding.UTF8, "application/json");

Based on this, seems jresp is a string. Easiest way would be to remove the square brackets from string:
JavaScript
jresp = jresp.substring(1, jresp.length-1);


If the json data was in array form, would have suggested take jsonarray[0] to get the intended data.
 
Share this answer
 
ControllerObject.Add(ClassObj);

var jresp = JsonConvert.SerializeObject(ControllerObject, Formatting.Indented);


You are converting ControllerObject into JSON to return, and given you are using .Add on it I assume it is a List or some other collection, so when you convert it to JSON you get the square brackets, even if there is only one item in the list. If you only want to return that one item then return ClassObj rather than ControllerObject

var jresp = JsonConvert.SerializeObject(ClassObj, Formatting.Indented);


Or return the first item in ControllerObject

var jresp = JsonConvert.SerializeObject(ControllerObject[0], Formatting.Indented);
 
Share this answer
 
Comments
Member 14938958 17-Sep-20 9:44am    
Even if I don't use collection/list and my classes are like:

public class Location
{
public string id { get; set; }
public int area { get; set; }
public int region { get; set; }

}

public class Root
{
public Location locations { get; set; }
}


I am getting JSON like:

[

{
"locations":
{
// some data
}
}

] // Why these [] ?

Want to remove extra square brackets at the beginning and the end JSON response.
F-ES Sitecore 17-Sep-20 10:01am    
You're getting the outer brackets as the object you are serialising to return is a collection (ControllerObject). To get rid of the brackets return a single object, like ControllerObject[0] or ClassObj as I suggested above.
Member 14938958 17-Sep-20 11:19am    
You are right. I used class object to resolve it. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900