如何从此json对象获取值?

时间:2019-05-28 18:45:45

标签: c# .net json

如何从此json获取值?

从API返回的json如下:

{
  "id": 191671,
  "callTrackingProfiles": {
    "1": {
      "destinationPhoneNumber": "8449000218",
      "phones": [
        {
          "phoneType": 3,
          "phoneNumber": "8443876176",
          "destinationWhisper": true,
          "callerWhisper": false,
          "isLocal": false,
          "recordCalls": false,
          "countryCode": "USA"
        },
        {
          "phoneType": 7,
          "phoneNumber": "8442692468",
          "destinationWhisper": true,
          "callerWhisper": false,
          "isLocal": false,
          "recordCalls": false,
          "countryCode": "USA"
        },
        {
          "phoneType": 10,
          "phoneNumber": "9493931848",
          "destinationWhisper": true,
          "callerWhisper": false,
          "isLocal": true,
          "recordCalls": false,
          "countryCode": "USA"
        }
      ]
    },
    "2": {
      "destinationPhoneNumber": "8449000218",
      "phones": [
        {
          "phoneType": 3,
          "phoneNumber": "9495283517",
          "destinationWhisper": true,
          "callerWhisper": false,
          "isLocal": true,
          "recordCalls": false,
          "countryCode": "USA"
        },
        {
          "phoneType": 7,
          "phoneNumber": "8443500663",
          "destinationWhisper": true,
          "callerWhisper": false,
          "isLocal": false,
          "recordCalls": false,
          "countryCode": "USA"
        }
      ]
    }
}

现在,我将其反序列化:

dynamic jsonResults = JsonConvert.DeserializeObject<dynamic>(response);
var profiles = jsonResults.callTrackingProfiles;

我可以使用foreach来循环它。

但是如何获取destinationPhoneNumber和phoneNumber的值?

foreach (var p in profiles)
{
    ...
    p.destinationPhoneNumber
    ...
}

这不起作用,并且p.1出现错误:

error CS1061: 'object' does not contain a definition for 'destinationPhoneNumber' and no accessible extension method 'destinationPhoneNumber' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)  

谢谢

2 个答案:

答案 0 :(得分:3)

我假设您正在使用视觉工作室。如果是这样,我认为这是最简单的解决方案:1.创建一个c#类。 2.然后复制所有JSON(从示例中)。 3.然后转到您之前创建的那个类,然后转到Edit> paste special> Paste JSON as Classes。

这将基于JSON为您创建一个类。然后,您要做的就是将其反序列化为该类类型,它将创建一个对象,您可以从中提取所有信息。希望对您有所帮助!

http://blog.codeinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-And-Xml/

enter image description here

答案 1 :(得分:2)

尝试创建一些定义JSON结构的类:

public class RequestVm
{
    public long Id { get; set; }
    public Dictionary<string, CallTrackingProfileVm> callTrackingProfiles { get; set; }
}

public class CallTrackingProfileVm
{
    public string destinationPhoneNumber { get; set; }
    public List<PhoneNumberVm> phones { get; set; }
}

public class PhoneNumberVm
{
    public int phoneType { get; set; }
    public string phoneNumber { get; set; }
    public bool destinationWhisper { get; set; }
    public bool isLocal { get; set; }
    public bool recordCalls { get; set; }
    public string countryCode { get; set; }
}

然后您可以像这样遍历它:

var jsonResults = JsonConvert.DeserializeObject<RequestVm>(str);
var profiles = jsonResults.callTrackingProfiles;
foreach(var value in profiles.Values)
{
    var phone = value.destinationPhoneNumber;
}