linqtotwitter retweets not appearing as retweets from stream - c#

I am using the linqtotwitter (v3.1.2) library to stream tweets into an app but we don't want to process retweets. When I received a retweet the properties retweeted and RetweetedStatus.ID are always false and 0.
Is this a bug in the library? I have looked at the source code in GitHub and it looks like it should work.
At the moment I just checking to see if the tweet starts with "RT " but that isn't 100% accurate of course.

This appears to be a bug in the library. I have coded around the issue by checking if the tweet text begins with "RT ". Not ideal but it worked for my purpose.

The Twitter API sets retweeted to false, which is why the Retweeted property in the returned Status entity is false. Given this, there are two things you can do to track Retweets:
You can check for the tweets starting with RT like you do because some people still do retweets like this, rather than clicking the Retweet button.
Look at the RetweetedStatus property of the Status entity. If it is a retweet, this will be a valid Status instance. If it is not a retweet, RetweetedStatus will be null.
Like I said, the Twitter API is sending false. Here's the raw JSON for a retweet:
{
"created_at": "Tue Dec 15 17:41:31 +0000 2015",
"id": 676819388972994560,
"id_str": "676819388972994560",
"text": "RT #Dotnetster: Favourites.io built with LinqToTwitter #JoeMayo. Search and tag your Twitter Favourites. https:\/\/t.co\/CBkJL4KS5Q",
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 15411837,
"id_str": "15411837",
"name": "Joe Mayo",
"screen_name": "JoeMayo",
"location": "Las Vegas, NV",
"description": "Author, Consultant, and Instructor. Microsoft .NET MVP. #VegasTech",
"url": "http:\/\/t.co\/8atdOuFAEx",
"entities": {
"url": {
"urls": [{
"url": "http:\/\/t.co\/8atdOuFAEx",
"expanded_url": "http:\/\/www.mayosoftware.com",
"display_url": "mayosoftware.com",
"indices": [0, 22]
}]
},
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 2578,
"friends_count": 274,
"listed_count": 174,
"created_at": "Sun Jul 13 04:35:50 +0000 2008",
"favourites_count": 12,
"utc_offset": -28800,
"time_zone": "Pacific Time (US & Canada)",
"geo_enabled": true,
"verified": false,
"statuses_count": 2984,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "FFFFFF",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme19\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme19\/bg.gif",
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/456514674377256960\/PiL4qsWR_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/456514674377256960\/PiL4qsWR_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/15411837\/1398199881",
"profile_link_color": "3B94D9",
"profile_sidebar_border_color": "FFFFFF",
"profile_sidebar_fill_color": "F6FFD1",
"profile_text_color": "333333",
"profile_use_background_image": false,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": true
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweeted_status": {
"created_at": "Tue Dec 15 17:34:26 +0000 2015",
"id": 676817605064290306,
"id_str": "676817605064290306",
"text": "Favourites.io built with LinqToTwitter #JoeMayo. Search and tag your Twitter Favourites. https:\/\/t.co\/CBkJL4KS5Q",
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 26903787,
"id_str": "26903787",
"name": "Ross McLoughlin",
"screen_name": "Dotnetster",
"location": "",
"description": "Favourites.io",
"url": "http:\/\/t.co\/acXDZ35G1d",
"entities": {
"url": {
"urls": [{
"url": "http:\/\/t.co\/acXDZ35G1d",
"expanded_url": "http:\/\/www.favourites.io",
"display_url": "favourites.io",
"indices": [0, 22]
}]
},
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 206,
"friends_count": 320,
"listed_count": 52,
"created_at": "Fri Mar 27 00:41:29 +0000 2009",
"favourites_count": 1393,
"utc_offset": 0,
"time_zone": "Dublin",
"geo_enabled": false,
"verified": false,
"statuses_count": 6662,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "000000",
"profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/182308454\/twitter.jpg",
"profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/182308454\/twitter.jpg",
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/459298001505099777\/lQd1OjeL_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/459298001505099777\/lQd1OjeL_normal.jpeg",
"profile_link_color": "0084B4",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": false,
"has_extended_profile": false,
"default_profile": false,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 4,
"favorite_count": 5,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [{
"screen_name": "JoeMayo",
"name": "Joe Mayo",
"id": 15411837,
"id_str": "15411837",
"indices": [39, 47]
}],
"urls": [],
"media": [{
"id": 676817604057497600,
"id_str": "676817604057497600",
"indices": [90, 113],
"media_url": "http:\/\/pbs.twimg.com\/media\/CWSKBmOUAAAqsr2.png",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/CWSKBmOUAAAqsr2.png",
"url": "https:\/\/t.co\/CBkJL4KS5Q",
"display_url": "pic.twitter.com\/CBkJL4KS5Q",
"expanded_url": "http:\/\/twitter.com\/Dotnetster\/status\/676817605064290306\/photo\/1",
"type": "photo",
"sizes": {
"large": {
"w": 1024,
"h": 535,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 600,
"h": 313,
"resize": "fit"
},
"small": {
"w": 340,
"h": 177,
"resize": "fit"
}
}
}]
},
"extended_entities": {
"media": [{
"id": 676817604057497600,
"id_str": "676817604057497600",
"indices": [90, 113],
"media_url": "http:\/\/pbs.twimg.com\/media\/CWSKBmOUAAAqsr2.png",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/CWSKBmOUAAAqsr2.png",
"url": "https:\/\/t.co\/CBkJL4KS5Q",
"display_url": "pic.twitter.com\/CBkJL4KS5Q",
"expanded_url": "http:\/\/twitter.com\/Dotnetster\/status\/676817605064290306\/photo\/1",
"type": "photo",
"sizes": {
"large": {
"w": 1024,
"h": 535,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 600,
"h": 313,
"resize": "fit"
},
"small": {
"w": 340,
"h": 177,
"resize": "fit"
}
}
}]
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
},
"is_quote_status": false,
"retweet_count": 4,
"favorite_count": 0,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [{
"screen_name": "Dotnetster",
"name": "Ross McLoughlin",
"id": 26903787,
"id_str": "26903787",
"indices": [3, 14]
}, {
"screen_name": "JoeMayo",
"name": "Joe Mayo",
"id": 15411837,
"id_str": "15411837",
"indices": [55, 63]
}],
"urls": [],
"media": [{
"id": 676817604057497600,
"id_str": "676817604057497600",
"indices": [106, 129],
"media_url": "http:\/\/pbs.twimg.com\/media\/CWSKBmOUAAAqsr2.png",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/CWSKBmOUAAAqsr2.png",
"url": "https:\/\/t.co\/CBkJL4KS5Q",
"display_url": "pic.twitter.com\/CBkJL4KS5Q",
"expanded_url": "http:\/\/twitter.com\/Dotnetster\/status\/676817605064290306\/photo\/1",
"type": "photo",
"sizes": {
"large": {
"w": 1024,
"h": 535,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 600,
"h": 313,
"resize": "fit"
},
"small": {
"w": 340,
"h": 177,
"resize": "fit"
}
},
"source_status_id": 676817605064290306,
"source_status_id_str": "676817605064290306",
"source_user_id": 26903787,
"source_user_id_str": "26903787"
}]
},
"extended_entities": {
"media": [{
"id": 676817604057497600,
"id_str": "676817604057497600",
"indices": [106, 129],
"media_url": "http:\/\/pbs.twimg.com\/media\/CWSKBmOUAAAqsr2.png",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/CWSKBmOUAAAqsr2.png",
"url": "https:\/\/t.co\/CBkJL4KS5Q",
"display_url": "pic.twitter.com\/CBkJL4KS5Q",
"expanded_url": "http:\/\/twitter.com\/Dotnetster\/status\/676817605064290306\/photo\/1",
"type": "photo",
"sizes": {
"large": {
"w": 1024,
"h": 535,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 600,
"h": 313,
"resize": "fit"
},
"small": {
"w": 340,
"h": 177,
"resize": "fit"
}
},
"source_status_id": 676817605064290306,
"source_status_id_str": "676817605064290306",
"source_user_id": 26903787,
"source_user_id_str": "26903787"
}]
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
}
Notice at the bottom of the JSON that retweeted is false, yet retweeted_status clearly has an associated JSON object. On this particular tweet, I did click the Twitter Retweet button, shown here:
https://twitter.com/Dotnetster/status/676817605064290306
Note: You can access the JSON of any query by examining the TwitterContext instance RawResult after the query completes.

Related

Desearalising JSON with arrays in arrays

I've spent a while trying to work out how to desterilise this JSON file and get each products
ID,NAME,COLLECTION COST, AND WEATHER ITS ON OFFER
Im currently attempting this with Newtonsoft.Json in a C# class.
could someone please point me in the right direction.
Many thanks
for the JSON text visit:
https://pastebin.com/bkQwpsAy
{
"_embedded": {
"products": [{
"uuid": "74f1501f-4a52-490a-b2b9-668f16e2db95",
"createdAt": "2020-04-20T13:44:22.000+00:00",
"itemId": "DRN543",
"altItemGroupId": "",
"popularityScore": 101.22,
"itemName": "Coca-Cola Bottles (GB) 6x1.5L",
"jsonFriendlyItemName": "Coca-Cola Bottles (GB) 6x1.5L",
"seoFriendlyItemName": "coca-cola-bottles-gb-6x1-5l",
"brand": "Coca Cola",
"imageLinks": ["https://jjproducts.global.ssl.fastly.net/jjfoodservice/image/upload/v1588074299/products/DRN543/_S/ggktoxjzbplky4uovec4.jpg"],
"price": 9.99,
"depth": 29.0,
"height": 32.0,
"itemNetWeight": 9.0,
"jadMobileItemName": "Coca Cola (GB) 6x1.5L",
"category1Id": "100005",
"category2Id": "200261",
"category3Id": "300194",
"category4Id": "400084",
"category5Id": "",
"category1Name": "Drinks",
"category2Name": "Soft Drinks",
"category3Name": "Fizzy Drinks",
"category4Name": "Cola",
"category5Name": "",
"origin": "United Kingdom",
"catchOrigin": "",
"productDescription": "",
"sellingPoints": "Coca Cola\nCocaCola",
"shelfLife": 135,
"sizeOrCut": "6x1.5l",
"qtyPerLayer": 20.0,
"standardPalletQty": 80.0,
"unitVolume": 17632.0,
"width": 19.0,
"allergensDeclaration": "",
"storageCondition": "Store cool and dry.",
"storedAt": "2020-11-03T23:52:28.789210Z",
"branches": [{
"locationId": "EN-MW",
"locationName": "Enfield Branch",
"warehouseArea": "DA",
"warehouseZone": "GZ"
}, {
"locationId": "LS-MW",
"locationName": "Leicester Branch",
"warehouseArea": "DA",
"warehouseZone": "GZ"
}
],
"branchesBeforeLastUpdate": [{
"locationId": "EN-MW",
"locationName": "Enfield Branch",
"warehouseArea": "DA",
"warehouseZone": "GZ"
}, {
"locationId": "LS-MW",
"locationName": "Leicester Branch",
"warehouseArea": "DA",
"warehouseZone": "GZ"
}
],
"video": [],
"categoryList": "[{\"id\":\"100005\",\"name\":\"Drinks\"},{\"id\":\"200261\",\"name\":\"Soft Drinks\"},{\"id\":\"300194\",\"name\":\"Fizzy Drinks\"}]",
"categoryId": "100005,200261,300194",
"categoryName": "Drinks,Soft Drinks,Fizzy Drinks",
"categoryNormalised": "[Cola Drinks Fizzy Soft]",
"productFeatures": ["Popular", "Ambient", "Vegan", "Vegetarian"],
"unitSize": "1.5L",
"unitPriceDivider": 0.16666,
"unitPriceTypeDisplayText": "each",
"offer": {
"itemId": "DRN543",
"promoForCc": false,
"promoTagId": "Monthly",
"promoTag": "Monthly Special Promotions",
"promoEnd": "31/12/2020",
"promoDisAmt": 0,
"promoDisPct": 0,
"promoDiscountText": [],
"id": "DRN543"
},
"delivery": {
"price": 8.29,
"priceInc": 8.29,
"unitPriceDisplay": "£1.38 each",
"step": 1.0,
"max": 15.0,
"collection": false
},
"collection": {
"price": 7.29,
"priceInc": 7.29,
"unitPriceDisplay": "£1.21 each",
"step": 1.0,
"max": 15.0,
"collection": true
},
"previouslyPurchased": false,
"favourite": false,
"available": true,
"new": false,
"popular": true,
"popularOnCategory1": true,
"popularOnCategory2": true,
"popularOnCategory3": true,
"ageRestriction": false,
"halal": false,
"vegan": true,
"vegeterian": true,
"numberOfPackage": 6,
"numberOfUnitsInPackage": 1.5,
"unitType": "litre",
"CCMAltItemGroup": "",
"JJeBrand": "Coca Cola",
"JadConsumableDepth": 0.0,
"JadConsumableHeight": 0.0,
"JadConsumableWidth": 0.0,
"JJeCategory1Id": "100005",
"JJeCategory2Id": "200261",
"JJeCategory3Id": "300194",
"JJeCategory4Id": "400084",
"JJeCategory5Id": "",
"JJeCategory1": "Drinks",
"JJeCategory2": "Soft Drinks",
"JJeCategory3": "Fizzy Drinks",
"JJeCategory4": "Cola",
"JJeCategory5": "",
"JJeCookingInstruction": "Best served chilled.",
"JJeIngredients": "Carbonated Water, Sugar, Colour (Caramel E150d), Phosphoric Acid, Natural Flavourings including Caffeine.",
"JadIngredientsHTML": "Carbonated Water, Sugar, Colour (Caramel E150d), Phosphoric Acid, Natural Flavourings including Caffeine.",
"JJeOrigin": "United Kingdom",
"JadCatchOrigin": "",
"JJeProductDescription": "",
"JJeSellingPoints": "Coca Cola\nCocaCola",
"JJeShelfLife": 135,
"JJeSizeOrCut": "6x1.5L",
"JadAllergensDeclaration": "",
"JadStorageCondition": "Store cool and dry.",
"JJeEnergyKJ": 180.0,
"JJeEnergyKCAL": 42.0,
"JJeFatG": "0",
"JadFatSaturatesG": "0",
"JJeCarbohydrateG": "10.6",
"JadCarbohydrateSugarsG": "10.6",
"JJeProteinG": "0",
"JadSodiumG": "0",
"IsAgeRestriction": false,
"IsHalal": false,
"IsVegan": true,
"IsVegeterian": true
}
]
},
"_links": {
"maintenance-message": {
"href": "[]"
},
"announcement-message": {
"href": "[]"
}
},
"page": {
"size": 12,
"totalElements": 18,
"totalPages": 2,
"number": 0
}
}
public static List<JJs.ITEMS> JJSGetProductHTML(String Term)
{
string url = "https://www.website.com/api/product-search-agg/api/v1/product/websearch?b=DG-MW&page=0&q=" + Term+ "&size=12&sortType=search&format=json";
WebClient WC = new WebClient();
string JSON = WC.DownloadString(url);
var onject,= JsonConvert.DeserializeObject<dynamic>(JSON);
string s = onject._embedded.products[0].ToString();
List<JJs.ITEMS> products = new List<JJs.ITEMS>();
foreach (var m in onject._embedded.products)
{
JJs.ITEMS newitem = new JJs.ITEMS();
newitem.Name = m.jsonFriendlyItemName.ToString();
newitem.itemId = m.itemId.ToString();
newitem.price = m.collection.price.ToString();
try
{ m.offer.ToString(); newitem.Promoend = m.offer.promoEnd.ToString(); newitem.Promo = true; }
catch
{ newitem.Promo = false; };
products.Add(newitem);
}
return products;
}
As I didnt want all the feilds, i serialised it then used ".jsonFriendlyItemName",".itemId".. to only get the values I was looking for
thanks #JaromandaX, # Peter B and #dbc for your help

Get Json Array Size

I have Two Json returned from Web API which contain dynamic size array "contacts" and "amenities".
Normally I will access the fixed size array "results" in Json with the following code as I am the on fix the size.
string json = _obj.GetJsonResult(url);
Hotel hotel = Newtonsoft.Json.JsonConvert.DeserializeObject<Hotel>(json);
hotelName = hotel.results[0].property_name;
I have tried the following Link and following code to get the dynamic array size back but it don't seem to be working.
var jObject = JObject.Parse(json);
JArray jArray = (JArray)jObject["hotel.results[0].contacts"];
int length = jArray.Count;
Json One
{
"results": [
{
"property_code": "FGPENOTP",
"property_name": "Olive Tree Penang",
"location": {
"latitude": 5.32708,
"longitude": 100.27944
},
"address": {
"line1": "No 76 Jalan Mahsuri",
"city": "Bayan Lepas",
"postal_code": "11950",
"country": "MY"
},
"total_price": {
"amount": "265.00",
"currency": "MYR"
},
"min_daily_rate": {
"amount": "250.00",
"currency": "MYR"
},
"contacts": [
{
"type": "PHONE",
"detail": "+6046377777"
},
{
"type": "FAX",
"detail": "+6046377788"
}
],
"amenities": [
{
"amenity": "RESTAURANT",
"ota_code": 76,
"description": "Restaurant"
},
{
"amenity": "ELEVATORS",
"ota_code": 33,
"description": "Elevators"
},
{
"amenity": "LAUNDRY_SERVICE",
"ota_code": 58,
"description": "Laundry/Valet service"
},
{
"amenity": "PARKING",
"ota_code": 68,
"description": "Parking"
},
{
"amenity": "ROOM_SERVICE",
"ota_code": 77,
"description": "Room service"
},
{
"amenity": "SAFE_DEPOSIT_BOX",
"ota_code": 78,
"description": "Safe deposit box"
},
{
"amenity": "OUTDOOR_POOL",
"ota_code": 66,
"description": "Outdoor pool"
},
{
"amenity": "BALLROOM",
"ota_code": 191,
"description": "Ballroom"
}
],
"awards": [
{
"provider": "Local Star Rating",
"rating": "4"
}
],
"images": [],
"rooms": [
{
"booking_code": "A1KBAR",
"room_type_code": "A1K",
"rate_plan_code": "BAR",
"total_amount": {
"amount": "265.00",
"currency": "MYR"
},
"rates": [
{
"start_date": "2018-06-15",
"end_date": "2018-06-16",
"currency_code": "MYR",
"price": 250
}
],
"descriptions": [
"BEST AVAILABLE RATE",
"DELUXE ROOM/ KING OR TWIN SIZED BED/ 28 SQM/",
"FREE WI FI"
],
"room_type_info": {
"room_type": "Room with bath - Superior",
"bed_type": "King",
"number_of_beds": "1"
},
"rate_type_code": "BAR"
}
],
"_links": {
"more_rooms_at_this_hotel": {
"href": "https://api.sandbox.amadeus.com/v1.2/hotels/FGPENOTP?apikey=pa3lM5UhJAZ0AJfzlZHcAKdiGVBA90wA&check_in=2018-06-15&check_out=2018-06-16&lang=EN¤cy=MYR&referrer=more_rooms_at_this_hotel"
}
}
}
]
}
Json Two
{
"results": [
{
"property_code": "WWKULAPT",
"property_name": "Sama Sama Hotel Klia",
"location": {
"latitude": 2.75803,
"longitude": 101.7041
},
"address": {
"line1": "Jalan Cta 4b",
"city": "Selangor",
"postal_code": "64000",
"country": "MY"
},
"total_price": {
"amount": "625.00",
"currency": "MYR"
},
"min_daily_rate": {
"amount": "625.00",
"currency": "MYR"
},
"contacts": [
{
"type": "PHONE",
"detail": "60-3-87873333"
},
{
"type": "FAX",
"detail": "60-3-87875555"
},
{
"type": "EMAIL",
"detail": "info#samasamahotels.com"
},
{
"type": "URL",
"detail": "www.samasamahotels.com"
}
],
"amenities": [
{
"amenity": "COFFEE_SHOP",
"ota_code": 20,
"description": "Coffee shop"
},
{
"amenity": "RESTAURANT",
"ota_code": 76,
"description": "Restaurant"
},
{
"amenity": "ACCESSIBLE_FACILITIES",
"ota_code": 47,
"description": "Accessible facilities"
},
{
"amenity": "BEAUTY_SALON",
"ota_code": 107,
"description": "Beauty shop/salon"
},
{
"amenity": "CAR_RENTAL",
"ota_code": 15,
"description": "Car rental desk"
},
{
"amenity": "CHILDREN_WELCOME",
"ota_code": 218,
"description": "Children welcome"
},
{
"amenity": "ELEVATORS",
"ota_code": 33,
"description": "Elevators"
},
{
"amenity": "CURRENCY_EXCHANGE",
"ota_code": 26,
"description": "Currency exchange"
},
{
"amenity": "EXECUTIVE_FLOOR",
"ota_code": 34,
"description": "Executive floor"
},
{
"amenity": "HOTSPOTS",
"ota_code": 221,
"description": "Hotspots"
},
{
"amenity": "INTERNET_PUBLIC_AREAS",
"ota_code": 178,
"description": "Internet access in public areas"
},
{
"amenity": "JACUZZI",
"ota_code": 55,
"description": "Jacuzzi"
},
{
"amenity": "LAUNDRY_SERVICE",
"ota_code": 58,
"description": "Laundry/Valet service"
},
{
"amenity": "MASSAGE_SERVICES",
"ota_code": 61,
"description": "Massage services"
},
{
"amenity": "NON_PORNOGRAPHIC_TV",
"ota_code": 220,
"description": "Hotel does not provide pornographic films/TV"
},
{
"amenity": "PARKING",
"ota_code": 68,
"description": "Parking"
},
{
"amenity": "SAUNA",
"ota_code": 79,
"description": "Sauna"
},
{
"amenity": "SPA",
"ota_code": 84,
"description": "Spa"
},
{
"amenity": "VALET_PARKING",
"ota_code": 97,
"description": "Valet parking "
},
{
"amenity": "",
"ota_code": 46,
"description": "Hairdresser/barber"
},
{
"amenity": "POOL",
"ota_code": 71,
"description": "Pool"
},
{
"amenity": "ROOM_SERVICE",
"ota_code": 77,
"description": "Room service"
},
{
"amenity": "SAFE_DEPOSIT_BOX",
"ota_code": 78,
"description": "Safe deposit box"
}
],
"awards": [
{
"provider": "OHG",
"rating": "Deluxe"
}
],
"images": [],
"rooms": [
{
"booking_code": "DR12ZZZ",
"room_type_code": "D2T",
"rate_plan_code": "PRO",
"total_amount": {
"amount": "625.00",
"currency": "MYR"
},
"rates": [
{
"start_date": "2018-06-15",
"end_date": "2018-06-16",
"currency_code": "MYR",
"price": 625
}
],
"descriptions": [
"Sama Sama Best Available Rate GDS",
"Deluxe Twin/AC/Free WiFi/LCD TV/36SQM/",
"NonSmoking/Work Desk/Safe/Minibar"
],
"room_type_info": {
"room_type": "Room with bath - Minimum",
"bed_type": "Twin",
"number_of_beds": "2"
},
"rate_type_code": "PRO"
}
],
"_links": {
"more_rooms_at_this_hotel": {
"href": "https://api.sandbox.amadeus.com/v1.2/hotels/WWKULAPT?apikey=pa3lM5UhJAZ0AJfzlZHcAKdiGVBA90wA&check_in=2018-06-15&check_out=2018-06-16&lang=EN¤cy=MYR&referrer=more_rooms_at_this_hotel"
}
}
}
]
}
Thanks for every members here who helped.
Since you are already deserializing your JSON, all you should have to do is call hotel.results[0].contacts.Count.
This will return an integer representing the amount of items in the contacts list.
If contacts is of the array datatype, you will need to use length instead of count.

Selenium - json - c#

var pre1 = driver.FindElementByTagName("pre").Text.Replace(#"\", "").Trim();
dynamic root = JsonConvert.DeserializeObject(pre1);
I have this JSON response:
{
"success": true,
"message": null,
"outright": false,
"eventId": 0,
"si": 111,
"leonard": [{
"catalog":[0,0,0,0,0,0],
"edit": 25965112,
"mkilo": {
"888;315;2;3;0": {
"id": 1000,
"description": "Car"
},
"888;316;2;4;0": {
"id": 1001,
"description": "Train"
},
"888;317;2;5;0": {
"id": 1002,
"description": "Airplane"
}
},
"ti": "008000",
"checkin": 254,
"searchCar": {
"id": 1000,
"description": "Car"
}
}],
"ti": 149498
}
verified with jsonlint
root.leonard[0].catalog.Count = 6 ---- > OK
but
root.leonard[0].mkilo.Count = null - -- Why?
I want to read the contents of mkilo.

how to JsonConvert.DeserializeObject two data

{
"pagination": {
"next_url": "https://api.instagram.com/v1/users/",
"next_max_id": "98740505131"
},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"videos": {
"low_bandwidth": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12787808_180458939000143_910172373_s.mp4",
"width": 480,
"height": 480
},
"standard_resolution": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12824263_607003579446709_1011139465_n.mp4",
"width": 640,
"height": 640
},
"low_resolution": {
"url": "https://scontent.cdninstagram.com/t50.2886-16/12787808_180458939000143_910172373_s.mp4",
"width": 480,
"height": 480
}
},
"tags": [],
"type": "video",
"location": null,
"comments": {
"count": 2,
"data": [
{
"created_time": 1457332172,
"text": "什麼東西",
"from": {
"username": "d86241",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11371189_421316874725117_327631552_a.jpg",
"id": 397355082,
"full_name": "Jhao-wei Hvang"
},
"id": 1200511729352353800
},
{
"created_time": 1457771205,
"text": "😀",
"from": {
"username": "realkikog",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11820496_1860868347487361_262727492_a.jpg",
"id": 530665716,
"full_name": "K I K O"
},
"id": 1204194607797938400
}
]
},
"filter": "Crema",
"created_time": 1457326470,
"link": "https://www.instagram.com/p/BCo546hPKpA/",
"likes": {
"count": 22,
"data": [
{
"username": "ladyyihan",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/10684228_590857344404221_1064502415_a.jpg",
"id": 38863087,
"full_name": "Yihan"
},
{
"username": "miding_cyh",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/10005160_844941595551352_2014300181_a.jpg",
"id": 226855180,
"full_name": "i😸米丁 Juri"
},
{
"username": "aikoyin1985",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/12093346_523536977821971_1279823341_a.jpg",
"id": 2228728531,
"full_name": "aiko1985"
},
{
"username": "sh1recheungg",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/927866_1722116411358109_618748252_a.jpg",
"id": 416683725,
"full_name": "Sh1reCheungg"
}
]
},
"images": {
"low_resolution": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "https://scontent.cdninstagram.com/t51.2885-15/e15/12446061_1590715141254039_2091776153_n.jpg?ig_cache_key=MTIwMDQ2MzkwMDQ3MDcxNjk5Mg%3D%3D.2",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": 1457326470,
"text": "測試東西一下",
"from": {
"username": "jiantai.cai",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/1515128_723721747740304_742397288_a.jpg",
"id": 1417858881,
"full_name": "Mars"
},
"id": 1200463904363030800
},
"user_has_liked": false,
"id": "1200463900470716992_1417858881",
"user": {
"username": "jiantai.cai",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/1515128_723721747740304_742397288_a.jpg",
"id": 1417858881,
"full_name": "Mars"
}
}
]
}
i try
var collection = JsonConvert.DeserializeObject(json);
BUT comments data always get null
like this image
enter image description here
how can i model building this json
i want can
foreach(var item in model){
data.comments.data.text
}
to view
Please help ,
I'm using Newtonsoft 8.0.2 and I was able to get what you are asking with this.
string input = File.ReadAllText("C:\\Public\\input.json");
dynamic collection = JsonConvert.DeserializeObject(input);
foreach (var dataItem in collection.data)
{
dynamic comments = dataItem.comments;
foreach (dynamic comment in comments.data)
{
string text = comment.text;
}
}
What you were missing was that you did not iterate through each comment object under your item.

How to count number of JSON elements in an array in C#

I'm worinkg with instagram API and when I'm receiving recent media with any hashtag by this template:
https://api.instagram.com/v1/tags/{hashtag}/media/recent
I'm receiving data like this:
{
"pagination": {
"next_max_tag_id": "any_number",
"deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead",
"next_max_id": "any_number",
"next_min_id": "any_number",
"min_tag_id": "any_number",
"next_url": "https://api.instagram.com/v1/tags/{hashtag}/media/recent?access_token={my_personal_access-token}"
},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"tags": [
"any_tag",
"any_tag1",
"any_tag2",
"any_tag3"
],
"type": "image",
"location": null,
"comments": {
"count": 0,
"data": []
},
"filter": "Normal",
"created_time": "any_number",
"link": "any_url",
"likes": {
"count": 0,
"data": []
},
"images": {
"low_resolution": {
"url": "any_url",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "any_url",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "any_url",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": "any_number",
"text": "any_content",
"from": {
"username": "any_username",
"profile_picture": "any_url",
"id": "any_number",
"full_name": "any_full_name"
},
"id": "any_number"
},
"user_has_liked": false,
"id": "any_number",
"user": {
"username": "any_username",
"profile_picture": "any_url",
"id": "any_number",
"full_name": "any_full_name"
}
},
and so on.
As You can see, object "data" is an Array, and further we can see object "tags", which is also array. how can I check number elements array of array in C#? i tried like this:
JArray items = (JArray)jsonData["data[0].tags"];
int length = items.Count;
but it doesn't work. I parse JSON like this:
dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(JSON_string);
var token = JToken.Parse(str);
var data = token.Value<JArray>("data");
var tags = data[0].Value<JArray>("tags");
var count = tags.Count;
You can also use a JsonPath:
var token = JToken.Parse(str);
var count = token.SelectTokens("$.data[0].tags[*]").Count();

Categories

Resources