0

I am using following code to parse JSON. I am getting exception.

using System.Collections.Generic;
using Newtonsoft.Json;
using RestSharp;
using WebAutomation.BDD.Data;

namespace WebAutomation.Utils
{
    public class RestApi
    {
        private static IRestResponse HttpGetResponse(string endPoint)
        {
            var client = new RestClient(endPoint);
            var request = new RestRequest(Method.GET);
            return client.Execute(request);
        }

        public static FeaturedMenuItems GetFeaturedMenuItems()
        {
            var a = HttpGetResponse("http://<DOMAIN>/api/web/menu/quick");
            return JsonConvert.DeserializeObject<FeaturedMenuItems>(a.Content);
        }
    }
}

I am using following class to parse it:

using System.Collections.Generic;

namespace WebAutomation.BDD.Data
{
    public class FeaturedMenu
    {
        public string MenuItemName { get; set; }
        public string MenuItemName_en { get; set; }
        public string MenuItemName_zh { get; set; }
        public string url { get; set; }
        public string LastModifiedDate { get; set; }
    }

    public class FeaturedMenuItems
    {
        public List<FeaturedMenu> Featured_Menu { get; set; }
    }
}

This is the JSON I get:

[
  {
    "Featured_Menu": [
      {
        "MenuItemName": "Today's Racing",
        "MenuItemName-en": "Today's Racing",
        "MenuItemName-zh": "今日赛事",
        "url": "/racing-betting/today",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "Tomorrow's Racing",
        "MenuItemName-en": "Tomorrow's Racing",
        "MenuItemName-zh": "明日赛事",
        "url": "/racing-betting/tomorrow",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "Upcoming Sports",
        "MenuItemName-en": "Upcoming Sports",
        "MenuItemName-zh": "近期赛事",
        "url": "/sports-betting/next-up",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "Live Centre",
        "MenuItemName-en": "Live Centre",
        "MenuItemName-zh": "即场投注",
        "url": "/live-in-play-betting",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "Multi Express",
        "MenuItemName-en": "Multi Express",
        "MenuItemName-zh": "Multi Express",
        "url": "/multi-express",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "AFL Finals",
        "MenuItemName-en": "AFL Finals",
        "MenuItemName-zh": "AFL Finals",
        "url": "/sports-betting/australian-rules/afl",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "NRL Finals",
        "MenuItemName-en": "NRL Finals",
        "MenuItemName-zh": "NRL Finals",
        "url": "/sports-betting/rugby-league/nrl/nrl-matches",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "MLB Matches",
        "MenuItemName-en": "MLB Matches",
        "MenuItemName-zh": "MLB Matches",
        "url": "/sports-betting/baseball/major-league-baseball/mlb-matches",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "NFL Matches",
        "MenuItemName-en": "NFL Matches",
        "MenuItemName-zh": "NFL Matches",
        "url": "/sports-betting/american-football/nfl/nfl-matches/",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "EPL Matches",
        "MenuItemName-en": "EPL Matches",
        "MenuItemName-zh": "EPL Matches",
        "url": "/sports-betting/soccer/united-kingdom/english-premier-league-matches",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "UEFA Europa League",
        "MenuItemName-en": "UEFA Europa League",
        "MenuItemName-zh": "UEFA Europa League",
        "url": "/sports-betting/soccer/uefa-competitions/europa-league-matches/",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "Golovkin v Alvarez",
        "MenuItemName-en": "Golovkin v Alvarez",
        "MenuItemName-zh": "Golovkin v Alvarez",
        "url": "/sports-betting/boxing/bouts/fights/gennady-golovkin-v-saul-alvarez-20170917-600896-21745986",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "Makybe Diva Stakes",
        "MenuItemName-en": "Makybe Diva Stakes",
        "MenuItemName-zh": "Makybe Diva Stakes",
        "url": "/racing-betting/horse-racing/flemington/20170916/race-7-704632-23756853",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "The Shorts",
        "MenuItemName-en": "The Shorts",
        "MenuItemName-zh": "The Shorts",
        "url": "/racing-betting/horse-racing/randwick/20170916/race-7-704601-23756572",
        "LastModifiedDate": "20170914111417"
      },
      {
        "MenuItemName": "Lotto",
        "MenuItemName-en": "Lotto",
        "MenuItemName-zh": "Lotto",
        "url": "/http://crownlotto.com.au",
        "LastModifiedDate": "20170914111417"
      }
    ]
  }
]

I am getting following exception:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WebAutomation.BDD.Data.FeaturedMenuItems' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.'

I am not able to figure out the issue. Any help will be highly appreciated.

Aby
  • 11
  • 1
  • 7
  • You need to deserialize to a `List` as explained in [Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly](https://stackoverflow.com/q/22557559/3744182). – dbc Sep 14 '17 at 05:12

1 Answers1

0

You are trying to deserialize an array of FeaturedMenuItems not a single FeaturedMenuItems. Try using:

JsonConvert.DeserializeObject<FeaturedMenuItems[]>(a.Content)
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357