尝试从Wordpress / WooCommerce的xampp安装中获取JSON对象时遇到了一些困难 .
所以我使用以下代码从WooCommerce获取JSON对象:
internal Product fetchProduct(string id, string _apiKey, string _oauth_secret)
{
string _url = "http://localhost:8081/reserve/wp-json/wp/v2/product/" + id;
var restClient = new RestClient(_url);
var request = new RestRequest(Method.GET);
//request.Resource = "{version}/token";
request.RequestFormat = DataFormat.Json;
IRestResponse response = restClient.Execute(request);
var resp = response.Content;
Product prod = JsonConvert.DeserializeObject<Product>(resp);
return prod;
}
并将其反序列化为此类:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RestTest.Objects
{
class Product
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("slug")]
public string Slug { get; set; }
[JsonProperty("permalink")]
public string Permalink { get; set; }
[JsonProperty("date_created")]
public System.DateTimeOffset DateCreated { get; set; }
[JsonProperty("date_created_gmt")]
public System.DateTimeOffset DateCreatedGmt { get; set; }
[JsonProperty("date_modified")]
public System.DateTimeOffset DateModified { get; set; }
[JsonProperty("date_modified_gmt")]
public System.DateTimeOffset DateModifiedGmt { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("featured")]
public bool Featured { get; set; }
[JsonProperty("catalog_visibility")]
public string CatalogVisibility { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("short_description")]
public string ShortDescription { get; set; }
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("price")]
public string Price { get; set; }
[JsonProperty("regular_price")]
public string RegularPrice { get; set; }
[JsonProperty("sale_price")]
public string SalePrice { get; set; }
[JsonProperty("date_on_sale_from")]
public object DateOnSaleFrom { get; set; }
[JsonProperty("date_on_sale_from_gmt")]
public object DateOnSaleFromGmt { get; set; }
[JsonProperty("date_on_sale_to")]
public object DateOnSaleTo { get; set; }
[JsonProperty("date_on_sale_to_gmt")]
public object DateOnSaleToGmt { get; set; }
[JsonProperty("price_html")]
public string PriceHtml { get; set; }
[JsonProperty("on_sale")]
public bool OnSale { get; set; }
[JsonProperty("purchasable")]
public bool Purchasable { get; set; }
[JsonProperty("total_sales")]
public long TotalSales { get; set; }
[JsonProperty("virtual")]
public bool Virtual { get; set; }
[JsonProperty("downloadable")]
public bool Downloadable { get; set; }
[JsonProperty("downloads")]
public object[] Downloads { get; set; }
[JsonProperty("download_limit")]
public long DownloadLimit { get; set; }
[JsonProperty("download_expiry")]
public long DownloadExpiry { get; set; }
[JsonProperty("external_url")]
public string ExternalUrl { get; set; }
[JsonProperty("button_text")]
public string ButtonText { get; set; }
[JsonProperty("tax_status")]
public string TaxStatus { get; set; }
[JsonProperty("tax_class")]
public string TaxClass { get; set; }
[JsonProperty("manage_stock")]
public bool ManageStock { get; set; }
[JsonProperty("stock_quantity")]
public object StockQuantity { get; set; }
[JsonProperty("in_stock")]
public bool InStock { get; set; }
[JsonProperty("backorders")]
public string Backorders { get; set; }
[JsonProperty("backorders_allowed")]
public bool BackordersAllowed { get; set; }
[JsonProperty("backordered")]
public bool Backordered { get; set; }
[JsonProperty("sold_individually")]
public bool SoldIndividually { get; set; }
[JsonProperty("weight")]
public string Weight { get; set; }
[JsonProperty("dimensions")]
public Dimensions Dimensions { get; set; }
[JsonProperty("shipping_required")]
public bool ShippingRequired { get; set; }
[JsonProperty("shipping_taxable")]
public bool ShippingTaxable { get; set; }
[JsonProperty("shipping_class")]
public string ShippingClass { get; set; }
[JsonProperty("shipping_class_id")]
public long ShippingClassId { get; set; }
[JsonProperty("reviews_allowed")]
public bool ReviewsAllowed { get; set; }
[JsonProperty("average_rating")]
public string AverageRating { get; set; }
[JsonProperty("rating_count")]
public long RatingCount { get; set; }
[JsonProperty("related_ids")]
public long[] RelatedIds { get; set; }
[JsonProperty("upsell_ids")]
public object[] UpsellIds { get; set; }
[JsonProperty("cross_sell_ids")]
public object[] CrossSellIds { get; set; }
[JsonProperty("parent_id")]
public long ParentId { get; set; }
[JsonProperty("purchase_note")]
public string PurchaseNote { get; set; }
[JsonProperty("categories")]
public Category[] Categories { get; set; }
[JsonProperty("tags")]
public object[] Tags { get; set; }
[JsonProperty("images")]
public Image[] Images { get; set; }
[JsonProperty("attributes")]
public object[] Attributes { get; set; }
[JsonProperty("default_attributes")]
public object[] DefaultAttributes { get; set; }
[JsonProperty("variations")]
public object[] Variations { get; set; }
[JsonProperty("grouped_products")]
public object[] GroupedProducts { get; set; }
[JsonProperty("menu_order")]
public long MenuOrder { get; set; }
[JsonProperty("meta_data")]
public object[] MetaData { get; set; }
[JsonProperty("_links")]
public Links Links { get; set; }
}
public partial class Category
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("slug")]
public string Slug { get; set; }
}
public partial class Dimensions
{
[JsonProperty("length")]
public string Length { get; set; }
[JsonProperty("width")]
public string Width { get; set; }
[JsonProperty("height")]
public string Height { get; set; }
}
public partial class Image
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("date_created")]
public System.DateTimeOffset DateCreated { get; set; }
[JsonProperty("date_created_gmt")]
public System.DateTimeOffset DateCreatedGmt { get; set; }
[JsonProperty("date_modified")]
public System.DateTimeOffset DateModified { get; set; }
[JsonProperty("date_modified_gmt")]
public System.DateTimeOffset DateModifiedGmt { get; set; }
[JsonProperty("src")]
public string Src { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("alt")]
public string Alt { get; set; }
[JsonProperty("position")]
public long Position { get; set; }
}
public partial class Collection
{
[JsonProperty("href")]
public string Href { get; set; }
}
public partial class Welcome
{
public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, RestTest.Objects.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, RestTest.Objects.Converter.Settings);
}
}
我遇到的问题是返回的JSON对象没有WooCommerce API文档所说的大多数对象 .
文档可以在这里找到:https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#retrieve-a-product
我的JSON响应实际上是这样的:
{“id”:25,“date”:“2018-04-16T20:32:32”,“date_gmt”:“2018-04-16T20:32:32”,“guid”:{“呈现”:“ HTTP://本地主机:8081 /备用/ post_type =产物&p = 25 “},” 修饰的 “:” 2018-04-16T20:32:32" , “modified_gmt”: “2018-04-16T20:32:32” “蛞蝓”:“为了转运许可”,“状态”:“发布”,“类型”:“产品”,“链接”:“HTTP://本地主机:8081 /备用/产品/订单转运子license /“,”title“:{”render“:”Order Transporter License“},”content“:{”rendered“:”“,”protected“:false},”excerpt“:{”rendered“:”“ , “保护”:假}, “featured_media”:0, “comment_status”: “开放”, “ping_status”: “关闭”, “模板”: “”, “元”:[], “_链接”:{”自 “:[{” HREF “:” HTTP://本地主机:8081 /备用/ WP-JSON / WP / V2 /产品/ 25 “}],” 收藏 “:[{” HREF “:” HTTP://本地主机:8081 /备用/约WP-JSON / WP / V2 /产物 “}]” “:[{” HREF “:” HTTP://本地主机:8081 /备用/ WP-JSON / WP / V2 /类型/产品 “}],” 回复 “:[{” 嵌入 “:真,” HREF “:” HTTP://本地主机:8081 /备用/ WP-JSON / WP / V2 /评论交= 25 “}],”?可湿性粉剂:附件 “:[{” HREF “:” HTTP://本地主机:8081 /备用/ WP-JSON / w的?P / V2 /媒体父= 25 “}],” 居里 “:[{” 名称 “:” WP”, “HREF”: “https://api.w.org/”, “模板” :真正}]}}
所以我的问题是......我做错了什么?我试图尽可能地遵循文档,并通过RestSharp传递我需要的信息,但它似乎失败了 .