JSON을 C # 개체 목록으로 역 직렬화

앤드류 매틱

Google Places API에서 JSON을 역 직렬화하려고합니다. 내 사용자 정의 클래스는 다음과 같이 설정되며 내 코드는 다음과 같습니다. 내 프로그램을 실행할 때 오류가 발생하지 않지만 내 장소 개체가 null입니다.

class PlacesDictionary
{

    public void placesDictionary()
    { }

    public Places GetPlaces()
    {
        Places places = new Places();

        string apiKey = "I have an apiKey";
        string googleUrl;
        googleUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=43.038902,-87.906474&radius=500&type=restaurant&name=cruise&key=" + apiKey;

        WebRequest request = WebRequest.Create(googleUrl);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";
        WebResponse response = request.GetResponse();
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        places = JsonConvert.DeserializeObject<Places>(responseFromServer);

        Console.WriteLine(responseFromServer);
        Console.ReadLine();
        reader.Close();
        dataStream.Close();
        response.Close();

        return places;
    }
}

public class Places
{
    public List<Place> places { get; set; }

    public class Place
    {
        public Geometry geometry { get; set; }
        public string icon { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public OpeningHours opening_hours { get; set; }
        public List<Photo> photos { get; set; }
        public string place_id { get; set; }
        public int price_level { get; set; }
        public double rating { get; set; }
        public string reference { get; set; }
        public string scope { get; set; }
        public List<string> types { get; set; }
        public string vicinity { get; set; }

        public class Location
        {
            public double lat { get; set; }
            public double lng { 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 Viewport
        {
            public Northeast northeast { get; set; }
            public Southwest southwest { get; set; }
        }

        public class Geometry
        {
            public Location location { get; set; }
            public Viewport viewport { get; set; }
        }

        public class OpeningHours
        {
            public bool open_now { get; set; }
            public List<object> weekday_text { get; set; }
        }

        public class Photo
        {
            public int height { get; set; }
            public List<string> html_attributions { get; set; }
            public string photo_reference { get; set; }
            public int width { get; set; }
        }
    }
}
하칸 손 메즈

나는 당신이 사용해야한다고 생각합니다

List<Place> places = JsonConvert.DeserializeObject<List<Place>>(responseFromServer);

대신에

places = JsonConvert.DeserializeObject<Places>(responseFromServer);

그리고 다음 줄을 제거하는 것을 잊지 마십시오

 Places places = new Places();

편집 : 전체 답변

     static void Main(string[] args)
     {
           string apiKey = "your api key";
           string googleUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=43.038902,-87.906474&radius=500&type=restaurant&name=cruise&key=" + apiKey;

            WebRequest request = WebRequest.Create(googleUrl);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            WebResponse response = request.GetResponse();
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            //StreamWriter wr = new StreamWriter("json.txt");
            //wr.WriteLine(responseFromServer);
            //wr.Flush();
            //To see what it is inside json
            Result results = JsonConvert.DeserializeObject<Result>(responseFromServer);

            Console.WriteLine(responseFromServer);
            Console.ReadLine();
            reader.Close();
            dataStream.Close();
            response.Close();

        }
    }

    public class Result
    {
        public List<HTMLAttribution> html_attributions { get; set; }
        public string next_page_token { get; set; }
        public List<Place> results { get; set; }
        public string status { get; set; }

        //Definations of Classes
        public class HTMLAttribution { } //I don't what it is. It is empty for your url.

        public class Place
        {
            public Geometry geometry { get; set; }
            public string icon { get; set; }
            public string id { get; set; }
            public string name { get; set; }
            public OpeningHours opening_hours { get; set; }
            public List<Photo> photos { get; set; }
            public string place_id { get; set; }
            public int price_level { get; set; }
            public double rating { get; set; }
            public string reference { get; set; }
            public string scope { get; set; }
            public List<string> types { get; set; }
            public string vicinity { get; set; }

            public class Geometry
            {
                public Location location { get; set; }
                public Viewport viewport { get; set; }
            }
            public class Location
            {
                public double lat { get; set; }
                public double lng { get; set; }
            }
            public class Viewport
            {
                public Northeast northeast { get; set; }
                public Southwest southwest { 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 OpeningHours
            {
                public bool open_now { get; set; }
                public List<object> weekday_text { get; set; }
            }
            public class Photo
            {
                public int height { get; set; }
                public List<string> html_attributions { get; set; }
                public string photo_reference { get; set; }
                public int width { get; set; }
            }
        }
    }

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

JSON 목록을 C #의 개체 목록으로 역 직렬화 할 수 없음

분류에서Dev

JSON 개체를 .Net 목록으로 역 직렬화 (C #)

분류에서Dev

JSON을 C # 개체로 역 직렬화

분류에서Dev

C # XML을 개체 목록으로 역 직렬화

분류에서Dev

값 목록을 C # 개체로 역 직렬화하는 방법

분류에서Dev

json 문자열을 .NET 개체 목록으로 역 직렬화

분류에서Dev

JSON 파일을 개체 C #으로 역 직렬화

분류에서Dev

json 문자열을 개체 C # .net으로 역 직렬화

분류에서Dev

JSON 배열을 C # 개체로 역 직렬화 (TFL API)

분류에서Dev

JSON 문자열을 C # 및 Unity의 개체로 역 직렬화

분류에서Dev

xml 개체 C #으로 역 직렬화

분류에서Dev

C #의 JSON 배열을 목록으로 역 직렬화

분류에서Dev

C #에서는 간단한 JSON 개체를 정수 목록으로 역 직렬화합니다.

분류에서Dev

JSON을 C # 개체로 역 직렬화-역 직렬화되는 데이터 없음

분류에서Dev

C #에서 JSON 개체 역 직렬화

분류에서Dev

콘텐츠 유형을 따라 동적 개체 JSON으로 역 직렬화-C #

분류에서Dev

개체 직렬화 후 list <object> C #으로 역 직렬화

분류에서Dev

C # HttpResponseMessage를 개체로 역 직렬화

분류에서Dev

C #에서 XML을 개체로 역 직렬화하는 방법

분류에서Dev

CSV 문자열을 C # 개체로 역 직렬화

분류에서Dev

동적 개체로 JSON 역 직렬화

분류에서Dev

XML을 개체로 역 직렬화

분류에서Dev

json.net이 개체 목록으로 역 직렬화 할 수 없음

분류에서Dev

JSON을 C # 개체로 역 직렬화하는 데 문제가 있습니다.

분류에서Dev

JSON 문자열을 C # 개체로 역 직렬화 할 때 빈 클래스

분류에서Dev

C # 목록으로 JSON 문자열 역 직렬화

분류에서Dev

C #에서 중첩 된 목록으로 JSON 역 직렬화

분류에서Dev

개체 목록을 다른 컬렉션 형식으로 역 직렬화

분류에서Dev

XML 파일을 개체 목록으로 역 직렬화하는 방법

Related 관련 기사

  1. 1

    JSON 목록을 C #의 개체 목록으로 역 직렬화 할 수 없음

  2. 2

    JSON 개체를 .Net 목록으로 역 직렬화 (C #)

  3. 3

    JSON을 C # 개체로 역 직렬화

  4. 4

    C # XML을 개체 목록으로 역 직렬화

  5. 5

    값 목록을 C # 개체로 역 직렬화하는 방법

  6. 6

    json 문자열을 .NET 개체 목록으로 역 직렬화

  7. 7

    JSON 파일을 개체 C #으로 역 직렬화

  8. 8

    json 문자열을 개체 C # .net으로 역 직렬화

  9. 9

    JSON 배열을 C # 개체로 역 직렬화 (TFL API)

  10. 10

    JSON 문자열을 C # 및 Unity의 개체로 역 직렬화

  11. 11

    xml 개체 C #으로 역 직렬화

  12. 12

    C #의 JSON 배열을 목록으로 역 직렬화

  13. 13

    C #에서는 간단한 JSON 개체를 정수 목록으로 역 직렬화합니다.

  14. 14

    JSON을 C # 개체로 역 직렬화-역 직렬화되는 데이터 없음

  15. 15

    C #에서 JSON 개체 역 직렬화

  16. 16

    콘텐츠 유형을 따라 동적 개체 JSON으로 역 직렬화-C #

  17. 17

    개체 직렬화 후 list <object> C #으로 역 직렬화

  18. 18

    C # HttpResponseMessage를 개체로 역 직렬화

  19. 19

    C #에서 XML을 개체로 역 직렬화하는 방법

  20. 20

    CSV 문자열을 C # 개체로 역 직렬화

  21. 21

    동적 개체로 JSON 역 직렬화

  22. 22

    XML을 개체로 역 직렬화

  23. 23

    json.net이 개체 목록으로 역 직렬화 할 수 없음

  24. 24

    JSON을 C # 개체로 역 직렬화하는 데 문제가 있습니다.

  25. 25

    JSON 문자열을 C # 개체로 역 직렬화 할 때 빈 클래스

  26. 26

    C # 목록으로 JSON 문자열 역 직렬화

  27. 27

    C #에서 중첩 된 목록으로 JSON 역 직렬화

  28. 28

    개체 목록을 다른 컬렉션 형식으로 역 직렬화

  29. 29

    XML 파일을 개체 목록으로 역 직렬화하는 방법

뜨겁다태그

보관