-1

I have called a web service and I can't receive the next JSON object. How can I get user fields from the JSON? .............................................................................................................................................................

    curl -X POST -H "Authorization: Bearer <Bearer>" -H "Content-Type: application/json"
    -d '{
    "user": {
              "name": "John",
              "last_name" : "Harrys",
              "user_name" : "JHarrys",
              "email": "JHarrys@noreply.com",
              "password" : "JHarrys1005",
            },
    "user": {
              "name": "Mike",
              "last_name" : "Hanlon",
              "user_name" : "MHanlon",
              "email": "MHanlon@noreply.com",
              "password" : "5",
            }
    }'
    "https://<api host and port>/<service>/createUsers"


    [OperationContract]
    [WebInvoke(UriTemplate = "createUsers", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    public String createUsers(User user)
    {
    //code
    }

[DataContract]
    public class User 
    {
        [DataMember]
        public string name;
        [DataMember]
        public string last_name;
        [DataMember]
        public string user_name;
        [DataMember]
        public string email;
        [DataMember]
        public string password;
    }
erickterri
  • 61
  • 1
  • 1
  • 7

1 Answers1

1

One of the easiest ways to parse json data is to use Newtonsoft's Json library. You'll make a class that will hold the data you want to parse...

public class Movie {
public string Name {get;set;}
public string ReleaseDate {get;set}
...
}

Then you get your json string and Deserialize it with JsonConvert.

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys
bwoogie
  • 4,339
  • 12
  • 39
  • 72