1

How to get values from this json?

The returned json from an API is like:

{
  "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"
        }
      ]
    }
}

Now, I deserialized it:

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

and I can use foreach to loop it.

But how can I get the values of destinationPhoneNumber and phoneNumber?

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

This does not work, and p.1 has error:

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?)  

Thanks

2
  • 1
    Not sure if this is the issue but your model "callTrackingProfiles" isn't an array. It contains json OBJECTS "1" and "2" so I'm not sure if looping is going to give you the results you want. Have you tried debugging and inspecting it to see what it looks like? Try making a structured model that can match your data instead of using dynamic Commented May 28, 2019 at 18:48
  • 2
    When your input follows a static structure why not just create a class that represents that data-structure and serialize to that instead of using dynamic? Commented May 28, 2019 at 18:50

2 Answers 2

3

I am assuming you're using visual studios. If so this is the easiest solution in my opinion: 1.Create a c# class. 2.Then Copy all of your JSON (from your example). 3.Then go to that class you created before and go to Edit>paste special>Paste JSON as Classes.

This will create a class for you based off that JSON. Then all you have to do is deserialize it into that class type and it will create an object which you can pull all the information out of. I hope that helps!

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

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

To add, if you're not using VS, you can use the online tool json2csharp.com.
thanks. Slothario seems better since it will parse the data into a dictionary and we do not need to care how many elements in it. The VS tool will parse fixed # of CallTrackingProfile
2

Try creating some classes that define the structure of your 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; }
}

Then you can iterate over it like so:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.