JSON에서 여러 개체 역 직렬화가 작동하지 않음

해양 헬리콥터

그래서 저는 Google Maps API를 사용하여 두 도시 사이의 거리를 얻으려고합니다. Json 형식을 보려면 여기로 이동하십시오. https://developers.google.com/maps/documentation/directions/start

따라서 My Class 객체는 다음과 같이 보입니다.

public class GeocodedWaypoint
{
    public string geocoder_status { get; set; }
    public string place_id { get; set; }
    public List<string> types { get; set; }
    public bool? partial_match { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Bounds
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

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 EndLocation
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class StartLocation
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Distance2
{
    public string text { get; set; }
    public int value { get; set; }
}

public class Duration2
{
    public string text { get; set; }
    public int value { get; set; }
}

public class EndLocation2
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Polyline
{
    public string points { get; set; }
}

public class StartLocation2
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Step
{
    public Distance2 distance { get; set; }
    public Duration2 duration { get; set; }
    public EndLocation2 end_location { get; set; }
    public string html_instructions { get; set; }
    public Polyline polyline { get; set; }
    public StartLocation2 start_location { get; set; }
    public string travel_mode { get; set; }
    public string maneuver { get; set; }
}

public class Leg
{
    public Distance distance { get; set; }
    public Duration duration { get; set; }
    public string end_address { get; set; }
    public EndLocation end_location { get; set; }
    public string start_address { get; set; }
    public StartLocation start_location { get; set; }
    public List<Step> steps { get; set; }
    public List<object> traffic_speed_entry { get; set; }
    public List<object> via_waypoint { get; set; }
}

public class OverviewPolyline
{
    public string points { get; set; }
}

public class Route
{
    public Bounds bounds { get; set; }
    public string copyrights { get; set; }
    public List<Leg> legs { get; set; }
    public OverviewPolyline overview_polyline { get; set; }
    public string summary { get; set; }
    public List<object> warnings { get; set; }
    public List<object> waypoint_order { get; set; }
}

public class RootObject
{
    public List<GeocodedWaypoint> geocoded_waypoints { get; set; }
    public List<Route> routes { get; set; }
    public string status { get; set; }
}

그리고 저는 이것으로 다리 오브젝트를 얻고 싶습니다.

 static async Task<Leg> GetLegAsync(string path)
    {
        Leg leg = null;
        HttpResponseMessage response = await client.GetAsync(path);
        if (response.IsSuccessStatusCode)
        {
            var json = await response.Content.ReadAsStringAsync();
            leg = JsonConvert.DeserializeObject<Leg>(json);

        }

        return leg;
    }

문제는 이것을 통해 다리 객체에 액세스 할 때 제대로 역 직렬화되지 않는다는 것입니다. 어리석은 간단한 경우 미안하지만 HTTP 클라이언트와 이와 같은 것들을 실제로 엉망으로 만들지 않았습니다. 나는 C #에 꽤 능숙하지만 기능성에 대한 과거의 많은 연구를하지 않았고 이것의 일부에 들어갔다.

편집 : 이것이 도움이된다면

 static async Task RunAsync(string pos1,string pos2)
    {
        string apiKey = "MUST_KEEP_HIDDEN!";

        client.BaseAddress = new Uri("https://maps.googleapis.com/maps/api/directions/json?origin="+pos1+"&destination="+pos2+"&key=" + apiKey);

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        //Console.ReadLine();
        Leg leg = await GetLegAsync("https://maps.googleapis.com/maps/api/directions/json?origin=" + pos1 + "&destination=" + pos2 + "4&key=" + apiKey);

        showinfo(leg);

    }

미리 감사드립니다

Jegtugado

quicktype.io를 사용 하여이 코드를 생성했습니다 .

jsonString Google Maps API의 API 응답입니다.

이 예에서는 다음 API를 사용했습니다.

https://maps.googleapis.com/maps/api/directions/json?origin=Disneyland&destination=Universal+Studios+Hollywood4&key=AIzaSyAgQqqNyauyRWXdXC1HajFHyLD1vp70FdM

// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var data = GettingStarted.FromJson(jsonString);
//
namespace QuickType
{
    using System;
    using System.Net;
    using System.Collections.Generic;

    using Newtonsoft.Json;

    public partial class GettingStarted
    {
        [JsonProperty("routes")]
        public Route[] Routes { get; set; }

        [JsonProperty("geocoded_waypoints")]
        public GeocodedWaypoint[] GeocodedWaypoints { get; set; }

        [JsonProperty("status")]
        public string Status { get; set; }
    }

    public partial class Route
    {
        [JsonProperty("overview_polyline")]
        public Polyline OverviewPolyline { get; set; }

        [JsonProperty("copyrights")]
        public string Copyrights { get; set; }

        [JsonProperty("bounds")]
        public Bounds Bounds { get; set; }

        [JsonProperty("legs")]
        public Leg[] Legs { get; set; }

        [JsonProperty("warnings")]
        public object[] Warnings { get; set; }

        [JsonProperty("summary")]
        public string Summary { get; set; }

        [JsonProperty("waypoint_order")]
        public object[] WaypointOrder { get; set; }
    }

    public partial class Polyline
    {
        [JsonProperty("points")]
        public string Points { get; set; }
    }

    public partial class Bounds
    {
        [JsonProperty("northeast")]
        public EndLocation Northeast { get; set; }

        [JsonProperty("southwest")]
        public EndLocation Southwest { get; set; }
    }

    public partial class EndLocation
    {
        [JsonProperty("lat")]
        public double Lat { get; set; }

        [JsonProperty("lng")]
        public double Lng { get; set; }
    }

    public partial class Leg
    {
        [JsonProperty("end_location")]
        public EndLocation EndLocation { get; set; }

        [JsonProperty("duration")]
        public Distance Duration { get; set; }

        [JsonProperty("distance")]
        public Distance Distance { get; set; }

        [JsonProperty("end_address")]
        public string EndAddress { get; set; }

        [JsonProperty("start_location")]
        public EndLocation StartLocation { get; set; }

        [JsonProperty("traffic_speed_entry")]
        public object[] TrafficSpeedEntry { get; set; }

        [JsonProperty("start_address")]
        public string StartAddress { get; set; }

        [JsonProperty("steps")]
        public Step[] Steps { get; set; }

        [JsonProperty("via_waypoint")]
        public object[] ViaWaypoint { get; set; }
    }

    public partial class Distance
    {
        [JsonProperty("text")]
        public string Text { get; set; }

        [JsonProperty("value")]
        public long Value { get; set; }
    }

    public partial class Step
    {
        [JsonProperty("html_instructions")]
        public string HtmlInstructions { get; set; }

        [JsonProperty("duration")]
        public Distance Duration { get; set; }

        [JsonProperty("distance")]
        public Distance Distance { get; set; }

        [JsonProperty("end_location")]
        public EndLocation EndLocation { get; set; }

        [JsonProperty("polyline")]
        public Polyline Polyline { get; set; }

        [JsonProperty("maneuver")]
        public string Maneuver { get; set; }

        [JsonProperty("start_location")]
        public EndLocation StartLocation { get; set; }

        [JsonProperty("travel_mode")]
        public string TravelMode { get; set; }
    }

    public partial class GeocodedWaypoint
    {
        [JsonProperty("partial_match")]
        public bool? PartialMatch { get; set; }

        [JsonProperty("geocoder_status")]
        public string GeocoderStatus { get; set; }

        [JsonProperty("place_id")]
        public string PlaceId { get; set; }

        [JsonProperty("types")]
        public string[] Types { get; set; }
    }

    public partial class GettingStarted
    {
        public static GettingStarted FromJson(string json) => JsonConvert.DeserializeObject<GettingStarted>(json, Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this GettingStarted self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }

    public class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
        };
    }
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

List <Object> 직렬화가 여러 개체에서 작동하지 않습니다.

분류에서Dev

Angular http의 JSON 개체가 제대로 역 직렬화되지 않음

분류에서Dev

하나의 클래스에서 가능한 여러 개체 이름으로 JSON 문자열 역 직렬화

분류에서Dev

COM을 사용하여 PHP에서 .Net 개체 역 직렬화

분류에서Dev

DataContractSerializer를 사용하여 개체에서 XML 역 직렬화

분류에서Dev

C #에서 Newtonsoft를 사용하여 중첩 된 JSON 개체의 역 직렬화

분류에서Dev

작동하지 않는 Gson을 사용하여 json 역 직렬화

분류에서Dev

JsonObject를 C # 개체로 역 직렬화하지 않음

분류에서Dev

JSON에서 /에서 중첩 된 파생 개체를 사용하여 C # 데이터 개체 역 직렬화

분류에서Dev

JSON 개체 역 직렬화 및 작업

분류에서Dev

동적 개체로 JSON 역 직렬화

분류에서Dev

JSON 직렬 변환기를 사용하여 Mongo DB 개체 ID 역 직렬화

분류에서Dev

URL에서 C #의 List <T>로 여러 Json 개체 역 직렬화

분류에서Dev

C #에서 비동기 역 직렬화 호출이 작동하지 않음

분류에서Dev

파일에서 개체를 역 직렬화 및 재 직렬화하는 데 문제가있는 C # JSON.net

분류에서Dev

@JsonTypeInfo 및 @JsonSubTypes를 사용하여 JSON을 다형성 개체 모델로 역 직렬화하지 않습니까?

분류에서Dev

C #에서 Newtonsoft를 사용하여 Json에서 중첩 된 개체의 효율적인 수동 역 직렬화

분류에서Dev

XML 역 직렬화가 작동하지 않음-XML 문서 (0, 0)에 오류가 있습니다.

분류에서Dev

여러 개체로 ArrayList 역 직렬화

분류에서Dev

JSON 개체 역 직렬화

분류에서Dev

C #에서 JSON 개체 역 직렬화

분류에서Dev

C #에서 JSON을 개체로 역 직렬화하면 값이 매핑되지 않습니다.

분류에서Dev

클라이언트에서 서버로 SignalR 보내기 개체가 올바르게 역 직렬화되지 않음

분류에서Dev

Spring 부팅에서 Jackson을 사용하여 날짜 개체 역 직렬화

분류에서Dev

동일한 JSON 내에서 두 개체를 직렬화 한 다음 Redis를 통해 수신 문자열에서 역 직렬화

분류에서Dev

DrawElements가 여러 개체에서 예상대로 작동하지 않습니다.

분류에서Dev

ASP.Net Core MVC로 마이그레이션 할 때 JSON 직렬화 / 역 직렬화가 작동하지 않음

분류에서Dev

다른 개체를 포함하는 json 개체 역 직렬화

분류에서Dev

컨테이너없이 JSON.NET을 사용하여 개체 역 직렬화

Related 관련 기사

  1. 1

    List <Object> 직렬화가 여러 개체에서 작동하지 않습니다.

  2. 2

    Angular http의 JSON 개체가 제대로 역 직렬화되지 않음

  3. 3

    하나의 클래스에서 가능한 여러 개체 이름으로 JSON 문자열 역 직렬화

  4. 4

    COM을 사용하여 PHP에서 .Net 개체 역 직렬화

  5. 5

    DataContractSerializer를 사용하여 개체에서 XML 역 직렬화

  6. 6

    C #에서 Newtonsoft를 사용하여 중첩 된 JSON 개체의 역 직렬화

  7. 7

    작동하지 않는 Gson을 사용하여 json 역 직렬화

  8. 8

    JsonObject를 C # 개체로 역 직렬화하지 않음

  9. 9

    JSON에서 /에서 중첩 된 파생 개체를 사용하여 C # 데이터 개체 역 직렬화

  10. 10

    JSON 개체 역 직렬화 및 작업

  11. 11

    동적 개체로 JSON 역 직렬화

  12. 12

    JSON 직렬 변환기를 사용하여 Mongo DB 개체 ID 역 직렬화

  13. 13

    URL에서 C #의 List <T>로 여러 Json 개체 역 직렬화

  14. 14

    C #에서 비동기 역 직렬화 호출이 작동하지 않음

  15. 15

    파일에서 개체를 역 직렬화 및 재 직렬화하는 데 문제가있는 C # JSON.net

  16. 16

    @JsonTypeInfo 및 @JsonSubTypes를 사용하여 JSON을 다형성 개체 모델로 역 직렬화하지 않습니까?

  17. 17

    C #에서 Newtonsoft를 사용하여 Json에서 중첩 된 개체의 효율적인 수동 역 직렬화

  18. 18

    XML 역 직렬화가 작동하지 않음-XML 문서 (0, 0)에 오류가 있습니다.

  19. 19

    여러 개체로 ArrayList 역 직렬화

  20. 20

    JSON 개체 역 직렬화

  21. 21

    C #에서 JSON 개체 역 직렬화

  22. 22

    C #에서 JSON을 개체로 역 직렬화하면 값이 매핑되지 않습니다.

  23. 23

    클라이언트에서 서버로 SignalR 보내기 개체가 올바르게 역 직렬화되지 않음

  24. 24

    Spring 부팅에서 Jackson을 사용하여 날짜 개체 역 직렬화

  25. 25

    동일한 JSON 내에서 두 개체를 직렬화 한 다음 Redis를 통해 수신 문자열에서 역 직렬화

  26. 26

    DrawElements가 여러 개체에서 예상대로 작동하지 않습니다.

  27. 27

    ASP.Net Core MVC로 마이그레이션 할 때 JSON 직렬화 / 역 직렬화가 작동하지 않음

  28. 28

    다른 개체를 포함하는 json 개체 역 직렬화

  29. 29

    컨테이너없이 JSON.NET을 사용하여 개체 역 직렬화

뜨겁다태그

보관