是否可以从JSON响应中获取参数的值

时间:2019-08-28 13:58:53

标签: c# json

这是我的第一篇文章。

我正在尝试将Shippo集成到我们的应用程序中,并希望从Json响应中获取特定的值。

我收到以下Json回复:

{
  "count": 3,
  "next": null,
  "previous": null,
  "results": [
    {
      "object_created": "2019-08-28T12:58:57.064Z",
      "object_id": "16b602e0ajdsk87c4313920bc5e3174XYZ",
      "object_owner": "some@email.com",
      "shipment": "bd62234e151244dab2b2152fdcd15e76",
      "attributes": [
        "FASTEST"
      ],
      "amount": "31.30",
      "currency": "USD",
      "amount_local": "31.30",
      "currency_local": "USD",
      "provider": "USPS",
      "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
      "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
      "servicelevel": {
        "name": "Priority Mail Express",
        "token": "usps_priority_express",
        "terms": ""
      },
      "estimated_days": 1,
      "arrives_by": null,
      "duration_terms": "Overnight delivery to most U.S. locations.",
      "messages": [],
      "carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
      "test": false,
      "zone": "4"
    },
    {
      "object_created": "2019-08-28T12:58:57.063Z",
      "object_id": "ebdee42047aa49a3b7e08b1903ea02ea",
      "object_owner": "some@email.com",
      "shipment": "bd62234e151244dab2b2152fdcd15e76",
      "attributes": [
        "BESTVALUE",
        "CHEAPEST"
      ],
      "amount": "7.49",
      "currency": "USD",
      "amount_local": "7.49",
      "currency_local": "USD",
      "provider": "USPS",
      "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
      "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
      "servicelevel": {
        "name": "Priority Mail",
        "token": "usps_priority",
        "terms": ""
      },
      "estimated_days": 2,
      "arrives_by": null,
      "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
      "messages": [],
      "carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
      "test": false,
      "zone": "4"
    },
    {
      "object_created": "2019-08-28T12:58:57.062Z",
      "object_id": "ad410a41c84940ee80eb30c41c507613",
      "object_owner": "some@email.com",
      "shipment": "bd62234e151244dab2b2152fdcd15e76",
      "attributes": [],
      "amount": "7.78",
      "currency": "USD",
      "amount_local": "7.78",
      "currency_local": "USD",
      "provider": "USPS",
      "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
      "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
      "servicelevel": {
        "name": "Parcel Select",
        "token": "usps_parcel_select",
        "terms": ""
      },
      "estimated_days": 7,
      "arrives_by": null,
      "duration_terms": "Delivery in 2 to 8 days.",
      "messages": [],
      "carrier_account": "4e1506b8b7f7449e90620967e45aa1e9",
      "test": false,
      "zone": "4"
    }
  ]
}

我正在使用此呼叫:

WebClient webClient = new WebClient();

webClient.Headers.Add("Authorization: ShippoToken " + authToken);

var result = JsonValue.Parse(webClient.DownloadString("https://api.goshippo.com/shipments/"+ theObject.ObjectId + "/rates/USD"));

我的问题是如何从响应中仅获取“金额”值?

谢谢您的帮助!

3 个答案:

答案 0 :(得分:2)

您可以使用 Newtonsofts JToken来代替将json布局重新创建为类模型。这样可以减少创建代码的次数:

var json = "your json data";

// Parse the whole json string
var obj = JObject.Parse(json);
// Extract the results data as JArray
var results = (JArray)obj["results"];

// Iterate over all array entries and get the amount property
foreach(var result in results)
{
    var amount = result["amount"].ToString();
    // Do something with the amount
}

答案 1 :(得分:1)

您可以基于json创建一些类,然后使用JsonConvert反序列化 例如https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

提示: 复制json并放入https://jsonformatter.curiousconcept.com/

之类的json查看器中

答案 2 :(得分:0)

使用https://www.newtonsoft.com/json反序列化json响应。如果您不希望这样做,可以使用一系列string.Split()来获取金额。但是使用newtonsoft(通过nuget软件包管理器)将是最简单的。