如何反序列化嵌套JSON并将其绑定到模型以在视图中表示

时间:2019-05-04 09:58:47

标签: asp.net json view model

我正在打电话给RestApi,这给了我嵌套的JSON作为响应。我必须处理此嵌套的Json并在同一Partial View上进行表示。我将分享我的模型,视图和控制器代码。

我创建了控制器来调用api并收集数据。它填充了根类,但没有填充子类

JSON:

{
    "id": 98,
    "title": "all numbers",
    "clues_count": 5,
    "clues": [
        {
            "id": 602,
            "answer": "<i>Eight</i>",
            "question": "It was \"enough\" for Dick van Patten",
            "value": 100,
            "airdate": "1984-09-14T12:00:00.000Z",
            "category_id": 98,
            "game_id": null,
            "invalid_count": null
        },
        {
            "id": 608,
            "answer": "16",
            "question": "A teen's \"sweet\" age",
            "value": 200,
            "airdate": "1984-09-14T12:00:00.000Z",
            "category_id": 98,
            "game_id": null,
            "invalid_count": null
        },
        {
            "id": 614,
            "answer": "<i>The Grapes of Wrath</i>",
            "question": "",
            "value": null,
            "airdate": "1984-09-14T12:00:00.000Z",
            "category_id": 98,
            "game_id": null,
            "invalid_count": null
        },
        {
            "id": 620,
            "answer": "Nathaniel Hawthorne",
            "question": "",
            "value": null,
            "airdate": "1984-09-14T12:00:00.000Z",
            "category_id": 98,
            "game_id": null,
            "invalid_count": null
        },
        {
            "id": 626,
            "answer": "F. Scott Fitzgerald",
            "question": "",
            "value": null,
            "airdate": "1984-09-14T12:00:00.000Z",
            "category_id": 98,
            "game_id": null,
            "invalid_count": 1
        }
    ]
}

型号:

public class Clue
{
    public int id { get; set; }
    public string answer { get; set; }
    public string question { get; set; }
    public int? value { get; set; }
    public DateTime airdate { get; set; }
    public int category_id { get; set; }
    public object game_id { get; set; }
    public int? invalid_count { get; set; }
}

public class RootObject
{
    public int id { get; set; }
    public string title { get; set; }
    public int clues_count { get; set; }
    public List<Clue> clues { get; set; }
}

控制器

   public async Task<ActionResult> Category( int id)
        {
            //  List<Category> clueInfo = new List<Category>();
            List<RootObject> cluess = new List<RootObject>();
            RootObject category = new RootObject();

            category.clueslist  = new List<Clues>();







            using (var client = new HttpClient())
            {
                //Passing service base url  
                client.BaseAddress = new Uri(Baseurl);

                client.DefaultRequestHeaders.Clear();
                //Define request data format  
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
                HttpResponseMessage Res = await client.GetAsync("api/category?id=" + id);
                Debug.WriteLine("My debug string here");

                //Checking the response is successful or not which is sent using HttpClient  
                if (Res.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api   
                    var ClueResponse = Res.Content.ReadAsStringAsync().Result;




                    //Deserializing the response recieved from web api and storing into the Employee list  
                     category = JsonConvert.DeserializeObject<RootObject>(ClueResponse);

                }






               // return View();
                return PartialView("Category", cluess);
            }

查看结果

title 
clues_count 

dining out 
20

0 个答案:

没有答案