1

Greetings i want to call a web api that returns a json string from From Controller and i want to map it on a class so i can save the data on the database.But i dont understand how to map each data on the json with the class. I only get the message that is succeed. my code is:

public ActionResult API()
{
    var client = new WebClient();
    var text = client.DownloadString("https://www.example.com/api/all-users?name=user%20&pass=password");
    wclients wclients = JsonConvert.DeserializeObject<wclients>(text);

    if (wclients.message == "success")
    {
        ViewBag.name = ("name = " + wclients.name);
    }

    return View();
}

my view is this:

@foreach (var item in ViewBag.name) {
<tr>
    <td>
       @item
    </td>


</tr>

}

The Response from web api is:

{"status":true,"message":"success","data":[{"name":"test test","email":"[email protected]","phone":"1234567890","affiliated_id":null,"account":{"real":[{"login":"1001175","pass":"4pJccK8AUWw8"},{"login":"3001180","pass":"4AeCqasw7jX2"},{"login":"3001182","pass":"fS2tf6Gsej7C"}]
12
  • have you tried something? Commented Aug 2, 2017 at 12:21
  • I am trying to get the value name so i have a list of names on the view, but what i get on wclients.name its null Commented Aug 2, 2017 at 12:23
  • share your js code. Commented Aug 2, 2017 at 12:23
  • I am not using any js, im trying to pass data through ViewBag. Commented Aug 2, 2017 at 12:24
  • share your view code then Commented Aug 2, 2017 at 12:25

3 Answers 3

1

try the below code:

public ActionResult API()
{
    var client = new WebClient();
    var text = client.DownloadString("https://www.example.com/api/all-users?
    name=user%20&pass=password");
    var wclients = JsonConvert.DeserializeObject<dynamic>(text);

        if (wclients.message == "success")
        {
            var data = wclients.data;
        }
      return View();
}
Sign up to request clarification or add additional context in comments.

16 Comments

wclients.data its not on my class property, and i dont see how with a property data i can get the list of users.
wclients contains the response you are getting from an api , right?
wclients its the class that i am using to map the properties together.
response you are getting from an api contains the data property, you can 't get the name without using the data. you can use var wclients = JsonConvert.DeserializeObject<wclients>(text); to access the data.
@EniSpetsi, i have edited my code changed text to text.data
|
0

wclients.name will never contain anything because there is not a property called "name" at the top level of the JSON object structure.

To access the name property you would need to enumerate through the data array (which should exist as a property on the wclients class) and get the name from there.

Update

As per the example below:

public class wclients
{
    public bool status { get; set; }

    public string message { get; set; }

    public IEnumerable<Account> data { get; set; }
}

public class Account
{
    public string name { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
}

public ActionResult API()
{
    var client = new WebClient();
    var text = client.DownloadString("https://www.example.com/api/all-users?name=user%20&pass=password");
    wclients clients = JsonConvert.DeserializeObject<wclients>(text);

    if (wclients.message == "success")
    {
        ViewBag.name = clients.data;
    }

    return View();
}

and in the view

@foreach (var item in ViewBag.name)
{
    <tr>
        <td>
           @item.name
        </td>
    </tr>
}

1 Comment

Sorry i did not understood that please can you share a example?
0

The JSON must map directly to your C# class.

For example, if the JSON has the following:

"name":"test test",
"email":"[email protected]"

Then your class (eg Person.cs) must have the same properties:

public class Person{
    public string name {get;set}
    public string email {get;set}
}

Then you would do:

Person person = JsonConvert.DeserializeObject<Person>(json);

In your example, you probably need wclients.data

if (wclients.message == "success")
{
    ViewBag.name = ("name = " + wclients.data.name);
}

Finally, the name wclients suggests there are multiple... in which case:

var names = new List<string>();
foreach(var client in wclients){
    if (client.message == "success")
    {
        names.Add("name = " + client.data.name);
    }
}

Comments

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.