1

I am able to return an JSON arrray from a WCF rest service.

[ {"Name" : "john", "age" : 23 }, {"Name" : "Petter",  "age" : 21 } ]

but how can I add a name to the array. In this case, I want to add the "People" to the beginning of the array

{"People" : [ {"Name" : "john", "age" : 23 }, {"Name" : "Petter",  "age" : 21 } ]}
3
  • What language are you using to generate the JSON string? Ideally you would change the object to what you want before converting it to a JSON string. Commented Feb 10, 2012 at 22:37
  • 1
    It's WCF, so C# or VB.NET are your most likely candidates Commented Feb 10, 2012 at 22:39
  • How are you returning JSON like this: [ {"Name" : "john", "age" : 23 }, {"Name" : "Petter", "age" : 21 } ]? I'm having the opposite problem: stackoverflow.com/questions/10073866/… Commented Apr 9, 2012 at 13:36

2 Answers 2

3

The easy way is change BodyStyle = WebMessageBodyStyle.Bare to BodyStyle = WebMessageBodyStyle.Wrapped.

The Wrapped enum member does it for you.

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

1 Comment

It works for me. Can you tell me how can I change json array name? Right now I get: {"WcfMethodName":[....]}.
0

return an object that has a people field whose value is an array of your name/age objects.

For example

[DataContract]
public class People
{
    [DataMember]
    public Person[] People { get; set; }
}

This assumes Person is the class you're returning in your array, for example:

[DataContract]
public class Person
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int age { get; set; }
}

2 Comments

Can't believe it is so easy :)
I just noticed that I left out the name of the property. that's fixed

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.