【问题标题】:Json string value doesn't give all dataJson 字符串值没有给出所有数据
【发布时间】:2018-04-25 03:10:52
【问题描述】:

这是我的 json 字符串

{
  "tracking_information": {
    "status_name": "Picked", 
    "status_code": "PCK", 
    "status_date": "2017-11-12T07:28:01.123272", 
    "source": "Web", 
    "status_date_local": "2017-11-12 11:28:01", 
    "status_description": "Picked up"
  }, 
  "order_information": {
    "tracking_no": "34120022", 
    "so_number": "44", 
    "client_ref": "Test Order"
  }
}

这是我的代码

byte[] json_orders = wc.DownloadData(url);

var bytesAsString = Encoding.ASCII.GetString(json_orders);

dynamic jsonObj = JsonConvert.DeserializeObject(bytesAsString);
foreach (var obj in jsonObj.tracking_information)
{
    string track_info = obj.Value;
}

obj 变量只保存状态名称值。如何获取状态码和相关信息。

【问题讨论】:

  • 我可以测试它,您可以在 tracking_information 部分中获得所有数据。只需在循环内添加一个 Console.WriteLine(obj.Value) 并查看结果
  • @Steve 我想一次性获取这些值。我只想检查对象的值
  • 顺便说一句,从 web 服务获取 ASCII 编码的 JSON 结果是非常不寻常的。你应该再仔细看看。我希望 UTF8 编码
  • 您是否有理由反序列化为动态(相对于具体类型)?

标签: c# asp.net json api


【解决方案1】:

不需要for循环。就这样试试吧;

var statusCode = jsonObj.tracking_information.status_code
var statusName= jsonObj.tracking_information.status_name

【讨论】:

    【解决方案2】:

    您的数据已经存在,您的检索方式存在问题, 您可以随时快速查看所有对象信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 2018-01-23
      • 1970-01-01
      • 2022-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      相关资源
      最近更新 更多