如何在c#中使用newtonsoft .json从json字符串中获取值

时间:2016-07-26 05:32:24

标签: c# json object json.net

我有跟随json字符串(这是它的一部分)。

SELECT 
COUNT(*) AS total
FROM 
(   SELECT ADDDATE('2016-07-01', INTERVAL @i:=@i+1 DAY) AS DAY
    FROM (
        SELECT a.a
        FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
    ) a
    JOIN (SELECT @i := -1) r1
    WHERE 
    @i < DATEDIFF('2016-08-01', '2016-07-01')

) AS dateTable
WHERE WEEKDAY(dateTable.Day) IN (5,6)

如何使用newtonsoft .json或任何其他方式从此字符串中获取值(单独的对象和值)。

我尝试过这样做(通过创建根对象,只需一点点尝试)

var cheerText = cheerio.load(res.text);
cheerText('#topic_list .topic_title').each(function (idx, element) {
  var element = cheerText(element);
  var href = url.resolve(cnodeUrl, element.attr('href'));
  topicUrls.push(href);
});

然后像这样调用它

[
  {
    "$id": "1",
    "topdealMaster": [
      {
        "$id": "2",
        "topdeal_id": 27,
        "calling_number": "12345678",
        "whats_include": "fssdf\nsdfsd",
        "child_policy": "sdfsdf\nsdf\nsdfsd",
        "optional_extras": "sdfsd\nsdfsd",
        "fine_prints": "sdfsdf\nsdf",
        "prices_id": "54,55,52,53,",
        "hotels_id": "2086,2086,",
        "cruise_id": "23,",
        "tour_id": "28,27,",
        "deal_id": 20224,
        "created_user_id": 28,
        "created_datetime": null,
        "last_modified_user_id": null,
        "last_modified_datetime": null,
        "isActive": null,
        "employee_master": null,
        "deal_master": {
          "$id": "3",
          "deal_id": 20224,
          "our_deal_code": "TCTZ00021",
          "deal_code": "TCTZ00021",
          "deal_company_id": null,
          "deal_title": "Thasnen testing",
          "created_user_id": null,
          "created_time": null,
          "isAlwaysOn": null,
          "isActive": null,
          "activated_user_id": null,
          "activated_time": null,
          "extra_info": null,
          "deal_type_id": "TCTZ",
          "min_price": null,
          "save_up": null,
          "fine_prints": null,
          "acco_type_id": null,
          "deal_hotel_id": null,
          "no_of_nigths": null,
          "airport_code": null,
          "deal_comments": [],
          "deal_fares": [],
          "deal_other_company_hotel": null,
          "topdeal_master": []
        }
      }
    ],
    "topDealHotel": [
      {
        "$id": "4",
        "Deal_Hotel_Id": 2086,
        "Deal_Hotel_Name": "The Ocean Colombo",
        "Created_Emp_Id": 108,
        "Updated_Emp_Id": 28,
        "Updated_Time": null,
        "System_Date_Time": "2016-06-20T10:11:19.037",
        "City_Code": "CMB",
        "Latitude": 6.88531,
        "Longitude": 79.855195,
        "LocationId": 6887404,
        "Hotel_Amentities": [
          {
            "$id": "5",
            "id": 0,
            "hotel_amenity_id": 2,
            "amenity_type": "24-hour front desk",
            "image_path": "fa fa-square"
          },
          {
            "$id": "6",
            "id": 0,
            "hotel_amenity_id": 3,
            "amenity_type": "Airport transportation",
            "image_path": "fa fa-square"
          },
          {
            "$id": "7",
            "id": 0,
            "hotel_amenity_id": 8,
            "amenity_type": "Bar/lounge",
            "image_path": "fa fa-square"
          },
          {
            "$id": "8",
            "id": 0,
            "hotel_amenity_id": 9,
            "amenity_type": "Breakfast available (surcharge)",
            "image_path": "fa fa-square"
          },
          {
            "$id": "9",
            "id": 0,
            "hotel_amenity_id": 10,
            "amenity_type": "Business center",
            "image_path": "fa fa-square"
          },
          {
            "$id": "10",
            "id": 0,
            "hotel_amenity_id": 16,
            "amenity_type": "Dry cleaning/laundry service",
            "image_path": "fa fa-square"
          },
          {
            "$id": "11",
            "id": 0,
            "hotel_amenity_id": 17,
            "amenity_type": "Elevator/lift",
            "image_path": "fa fa-square"
          },
          {
            "$id": "12",
            "id": 0,
            "hotel_amenity_id": 22,
            "amenity_type": "Free newspapers in lobby",
            "image_path": "fa fa-square"
          },
          {
            "$id": "13",
            "id": 0,
            "hotel_amenity_id": 23,
            "amenity_type": "Free WiFi",
            "image_path": "fa fa-square"
          },
          {
            "$id": "14",
            "id": 0,
            "hotel_amenity_id": 32,
            "amenity_type": "Limo or Town Car service available",
            "image_path": "fa fa-square"
          },
          {
            "$id": "15",
            "id": 0,
            "hotel_amenity_id": 33,
            "amenity_type": "Luggage storage",
            "image_path": "fa fa-square"
          },
          {
            "$id": "16",
            "id": 0,
            "hotel_amenity_id": 39,
            "amenity_type": "Restaurant",
            "image_path": "fa fa-square"
          },
          {
            "$id": "17",
            "id": 0,
            "hotel_amenity_id": 43,
            "amenity_type": "Smoke-free property",
            "image_path": "fa fa-square"
          }
        ],
        "Room_Amentities": [
          {
            "$id": "18",
            "id": 0,
            "room_amenity_id": 2,
            "amenity_type": "Air conditioning",
            "ImagePath": "fa fa-square"
          },
          {
            "$id": "19",
            "id": 0,
            "room_amenity_id": 4,
            "amenity_type": "Blackout drapes/curtains",
            "ImagePath": "fa fa-square"
          },

并试着这个,打电话到上课。

 public class RootobjectOne
        {
            [JsonProperty("topdealMaster")]
            public TopdealMaster TopdealMaster { get; set; }
        }

        public class TopdealMaster
        {
            [JsonProperty("topdeal_id")]
            public string topdealId {get;set;}

        }

但没有取得任何成功的结果。请帮助。

注意:它会出现以下错误-----&gt;因为类型需要JSON对象(例如{&#34; name&#34;:&#34; value&#34;})才能正确反序列化。 要修复此错误,请将JSON更改为JSON对象(例如{&#34; name&#34;:&#34; value&#34;})或将反序列化类型更改为实现集合接口的数组或类型(例如ICollection,IList)就像可以从JSON数组反序列化的List。 JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化。

1 个答案:

答案 0 :(得分:1)

你在顶级有一个数组,所以它应该是:

var items = JsonConvert.DeserializeObject<List<RootobjectOne>>(b);

同样"topdealMaster"是一个数组,所以根类应该是这样的:

public class RootobjectOne
{
    [JsonProperty("topdealMaster")]
    public Topdealmaster[] TopdealMasters { get; set; }
}