none
Is there a way to structure the JSON created? RRS feed

  • Question

  • I need to return JSON in the form:

    {
        "success": true,
        "children": [
            { "id": 1, "name": "Phil", "leaf": true },
            { "id": 2, "name": "Nico", "expanded": true, "children": [
                { "id": 3, "name": "Mitchell", "leaf": true }
            ]},
            { "id": 4, "name": "Sue", "loaded": true }
        ]
    }

    I can create an object composed of a success and children property. However, is there a way to build this without creating the return wrapper class for everything I have to return?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Wednesday, July 10, 2013 10:59 PM

Answers

  • Hi,

    Please try to refer to the following example:

    [DataContract]
    public class Person
    {
        [DataMember]
        public string FirstName { get; set; }
    
        [DataMember]
        public string LastName { get; set; }
    
        [DataMember]
        public int Age { get; set; }
    
        public Person(string firstName, string lastName, int age)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
        }
    }

    The service that is responsible for returning the content is defined as:

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TestService
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public string GetResults()
        {
            List<Person> results = new List<Person>();
            results.Add(new Person("Peyton", "Manning", 35));
            results.Add(new Person("Drew", "Brees", 31));
            results.Add(new Person("Tony", "Romo", 29));
    
            // Serialize the results as JSON
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(results.GetType());
            MemoryStream memoryStream = new MemoryStream();
            serializer.WriteObject(memoryStream, results);
    
            // Return the results serialized as JSON
            string json = Encoding.Default.GetString(memoryStream.ToArray());
            return json;
        }
    }


    Hope it can help you.

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, July 17, 2013 2:42 AM
    Moderator

All replies

  • Hi,

    Please try to use BodyStyle = WebMessageBodyStyle.Bare which will not wrap the request or response:

    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare......

    Or you can use BodyStyle = WebMessageBodyStyle.WrappedRequest which will wrap the request but not the response:

    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest......

    For more information, please try to refer to:
    #WebMessageBodyStyle:
    http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webmessagebodystyle.aspx .

    Hope it can help you.

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, July 11, 2013 4:31 AM
    Moderator
  • I added "BodyStyle = WebMessageBodyStyle.WrappedResponse" and now get:

    {"GetTextResult":"Hi there"}

    which is not what I'm looking for. That is just making the result a value for a key that is the function name. I need it turned into a 2 property JSON object. Is there a way to do that?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Thursday, July 11, 2013 3:36 PM
  • Hi,

    Please try to refer to the following example:

    [DataContract]
    public class Person
    {
        [DataMember]
        public string FirstName { get; set; }
    
        [DataMember]
        public string LastName { get; set; }
    
        [DataMember]
        public int Age { get; set; }
    
        public Person(string firstName, string lastName, int age)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
        }
    }

    The service that is responsible for returning the content is defined as:

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TestService
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public string GetResults()
        {
            List<Person> results = new List<Person>();
            results.Add(new Person("Peyton", "Manning", 35));
            results.Add(new Person("Drew", "Brees", 31));
            results.Add(new Person("Tony", "Romo", 29));
    
            // Serialize the results as JSON
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(results.GetType());
            MemoryStream memoryStream = new MemoryStream();
            serializer.WriteObject(memoryStream, results);
    
            // Return the results serialized as JSON
            string json = Encoding.Default.GetString(memoryStream.ToArray());
            return json;
        }
    }


    Hope it can help you.

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, July 17, 2013 2:42 AM
    Moderator
  • Wednesday, July 17, 2013 3:00 PM