Deserialize Complex JSON response Andoid C#

Hey Guys

Im really stuck with this one.
Im using the Google DistanceMatrix API
As soon I made the API call:

I get the following result:

I used Json2Csharp to create my model:

The model is in the same file.
So my problem is that I want to access the text and the value in the Distance class trough the root object.
Somehow I can not access this one. Can someone help me or provide a code snippet for this?

Many thanks in advance

Best Answer

Answers

  • FIISHxMANFIISHxMAN CHMember

    Here is the code in text:

    `using Android.App;
    using Android.Widget;
    using Android.OS;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using System.Net.Http;
    using System;
    using Newtonsoft.Json;
    using System.Net;
    using Android.Views;
    using System.Json;
    using Newtonsoft.Json.Serialization;

    namespace RadarSys {
    [Activity(Label = "RadarSys", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity {

        protected override void OnCreate(Bundle bundle) {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
    
            var deserializeJSON = selectFromApi();
    
        }
    
        private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {
            throw new NotImplementedException();
        }
    
        public RootObject selectFromApi() {
            string url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=API_KEY";
    
            var client = new WebClient();
            var content = client.DownloadString(url);
    
            RootObject json = JsonConvert.DeserializeObject<RootObject>(content);
    
            return json;
        }
    
    }
    
    public class Distance {
            public string text { get; set; }
            public int value { get; set; }
        }
    
        public class Duration {
            public string text { get; set; }
            public int value { get; set; }
        }
    
        public class Element {
            public Distance distance { get; set; }
            public Duration duration { get; set; }
            public string status { get; set; }
        }
    
        public class Row {
            public List<Element> elements { get; set; }
        }
    
        public class RootObject {
            public List<string> destination_addresses { get; set; }
            public List<string> origin_addresses { get; set; }
            public List<Row> rows { get; set; }
            public string status { get; set; }
        }
    

    }

    `

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Sorry, but I have not understand your problem.

    Second, instead of post images, you should post your code directly in the post, otherwise people can't copy/paste your code

  • FIISHxMANFIISHxMAN CHMember

    @AlessandroCaliaro said:
    Sorry, but I have not understand your problem.

    Second, instead of post images, you should post your code directly in the post, otherwise people can't copy/paste your code

    I want to access the Distance text and Distance value from my model.
    The reason why I posted the images is because the formating was bad so I posted images and then the code as a comment.

  • FIISHxMANFIISHxMAN CHMember

    @AlessandroCaliaro said:
    You are using List so to access Distance.text and Distance.value you should loop through your list

    json.rows[0].elements[0].distance.text // for example...

    Thank you very much. This solved my problem!

  • RobertMouldRobertMould USMember
    edited August 2017

    Hi Manuel,
    Could you post the code snippet of what you changed to get it working?
    I'm having the same issue but not quite sure how to implement the answer given above...

    Thanks,
    Rob

  • Esaavedra89Esaavedra89 USMember ✭✭

    @AlessandroCaliaro said:
    You are using List so to access Distance.text and Distance.value you should loop through your list

    json.rows[0].elements[0].distance.text // for example...

    Very useful, thanks!

Sign In or Register to comment.