1

当我收到来自 google maps API 的请求时,我使用此方法获取 json:

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataRequest options: NSJSONReadingAllowFragments error: nil];

jsons 看起来像这样:

{
routes =     (
            {
        bounds =             {
            northeast =                 {
                lat = "50.90961";
                lng = "20.6492";
            };
            southwest =                 {
                lat = "50.05775000000001";
                lng = "19.94544";
            };
        };
        copyrights = "Dane do Mapy \U00a92012 Google";
        legs =             (
                            {
                distance =                     {
                    text = "126 km";
                    value = 126118;
                };
                duration =                     {
                    text = "2 godz. 0 min";
                    value = 7229;
                };
                "end_address" = "Aleja Powstania Warszawskiego 15, 30-001 Krak\U00f3w, Polska";
                "end_location" =                     {
                    lat = "50.06";
                    lng = "19.9589";
                };
                "start_address" = "Mas\U0142owska 2, 25-001 Kielce, Polska";
                "start_location" =                     {
                    lat = "50.88884";
                    lng = "20.6492";
                };
                steps =                     (
                                            {
                        distance =                             {
                            text = "0,1 km";
                            value = 109;
                        };
                        duration =                             {
                            text = "1 min";
                            value = 39;
                        };
                        "end_location" =                             {
                            lat = "50.88824";
                            lng = "20.64796";
                        };
                        "html_instructions" = "Kieruj si\U0119 <b>Mas\U0142owska</b> na <b>po\U0142udniowy zach\U00f3d</b> w stron\U0119 <b>Szyd\U0142\U00f3wek G\U00f3rny</b>";
                        polyline =                             {
                            points = "gfruHo``}BvBvF";
                        };
                        "start_location" =                             {
                            lat = "50.88884";
                            lng = "20.6492";
                        };
                        "travel_mode" = DRIVING;
                    },
                                            {
                        distance =                             {
                            text = "0,2 km";
                            value = 221;
                        };
                        duration =                             {
                            text = "2 min";
                            value = 97;
                        };
                        "end_location" =                             {
                            lat = "50.88896";
                            lng = "20.64503";
                        };
                        "html_instructions" = "Skr\U0119\U0107 <b>w prawo</b> w <b>Szyd\U0142\U00f3wek G\U00f3rny</b>";
                        polyline =                             {
                            points = "obruHwx_}BaCpOMv@";
                        };
                        "start_location" =                             {
                            lat = "50.88824";
                            lng = "20.64796";
                        };
                        "travel_mode" = DRIVING;
                    },
                                            {
                        distance =                             {
                            text = "0,6 km";
                            value = 596;
                        };
                        duration =                             {
                            text = "1 min";
                            value = 56;
                        };
                        "end_location" =                             {
                            lat = "50.88427";
                            lng = "20.64091";
                        };
                        "html_instructions" = "Skr\U0119\U0107 <b>w lewo</b> w <b>Warszawska</b>";
                        polyline =                             {
                            points = "_gruHmf_}BrGvFjQ`OhA|@";
                        };
                        "start_location" =                             {
                            lat = "50.88896";
                            lng = "20.64503";
                        };
                        "travel_mode" = DRIVING;
                    },
                                            {
                        distance =                             {
                            text = "1,0 km";
                            value = 950;
                        };
                        duration =                             {
                            text = "2 min";
                            value = 104;
                        };
                        "end_location" =                             {
                            lat = "50.88779";
                            lng = "20.62863";
                        };
                        "html_instructions" = "Skr\U0119\U0107 w drug\U0105 <b>w prawo</b> w kierunku <b>Jesionowa</b>";
                        polyline =                             {
                            points = "uiquHul~|BiEvScCzKs@xCmBhK_@dD}@dIk@tBYr@Mf@Op@MnA";
                        };
                        "start_location" =                             {
                            lat = "50.88427";
                            lng = "20.64091";
                        };
                        "travel_mode" = DRIVING;
                    },
                                            {
                        distance =                             {
                            text = "4,2 km";
                            value = 4185;
                        };
                        duration =                             {
                            text = "5 min";
                            value = 292;
                        };
                        "end_location" =                             {
                            lat = "50.90806000000001";
                            lng = "20.58021";
                        };
                        "html_instructions" = "Na rondzie zjazd nr <b>1</b> w <b>\U0141\U00f3dzka</b>";
                        polyline =                             {
                            points = "u_ruH}_||BCd@Cf@?N@RDPHJJHsBzQIl@OrAm@bEEV[vBa@fBUfA_A~Bk@jA}DvGyDpEu@fAsA`C_AhBeAlCmAhDgAnDuAvEwAzFyAvGgA|F]bDH`Ba@fCQjA]tBi@hC}@zDsB|GeBdDwD|Gg@~@cAhBGJ_H`Mo@jAiB`DaAlBcBfD_@`A[fACDuCrMcC`L?@{AhHw@rDiDhPSx@e@dBc@vAa@fAi@jAgCfF{@rAe@p@A@m@p@kBhB";
                        };
                        "start_location" =                             {
                            lat = "50.88779";
                            lng = "20.62863";
                        };
                        "travel_mode" = DRIVING;
                    },
                                            {
                        distance =                             {
                            text = "0,2 km";
                            value = 213;
                        };
                        duration =                             {
                            text = "1 min";
                            value = 54;
                        };
                        "end_location" =                             {
                            lat = "50.90968";
                            lng = "20.57859";
                        };
                        "html_instructions" = "Kontynuuj wzd\U0142u\U017c <b>Trasa 74</b>";
                        polyline =                             {
                            points = "k~uuHiqr|BkBzAiEtEMP";
                        };
                        "start_location" =                             {
                            lat = "50.90806000000001";
                            lng = "20.58021";
                        };
                        "travel_mode" = DRIVING;
                    },
                                            {
                        distance =                             {
                            text = "92,8 km";
                            value = 92776;
                        };
                        duration =                             {
                            text = "1 godz. 17 min";
                            value = 4641;
                        };
                        "end_location" =                             {
                            lat = "50.24777";
                            lng = "20.08501";
                        };
                        "html_instructions" = "Skr\U0119\U0107 <b>w lewo</b> w <b>E77</b>";
......
status = OK;
}

接下来我使用这种方法来获取“点”键的值:

NSArray *items = [jsonArray valueForKeyPath:@"routes.legs.steps.polyline.points"];

当我想打印数组元素时,它只给出一个元素(这很奇怪,因为我在 json 中有 25 个点),结果看起来像这样:

(
    (
    "gfruHo``}BvBvF",
    "obruHwx_}BaCpOMv@",
    "_gruHmf_}BrGvFjQ`OhA|@",
    "uiquHul~|BiEvScCzKs@xCmBhK_@dD}@dIk@tBYr@Mf@Op@MnA",
    "u_ruH}_||BCd@Cf@?N@RDPHJJHsBzQIl@OrAm@bEEV[vBa@fBUfA_A~Bk@jA}DvGyDpEu@fAsA`C_AhBeAlCmAhDgAnDuAvEwAzFyAvGgA|F]bDH`Ba@fCQjA]tBi@hC}@zDsB|GeBdDwD|Gg@~@cAhBGJ_H`Mo@jAiB`DaAlBcBfD_@`A[fACDuCrMcC`L?@{AhHw@rDiDhPSx@e@dBc@vAa@fAi@jAgCfF{@rAe@p@A@m@p@kBhB",
    "k~uuHiqr|BkBzAiEtEMP",
    "ohvuHegr|Bl@rCN`@Xf@n@h@xAx@hAz@nAdCtF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y@t@q@",
    "mrqpHkayxBlCkAlAi@lAo@v@e@t@e@tC{@^InDu@fAW|@GP@r@BdBZxBn@`IfBlAb@fAv@",
    "k~opHceyxBd@j@TTPt@FT",
    "u{opHu`yxBoBTu@BuHl@o@BI@",
    "klppHg~xxBMqFxAI"
)
)

如何获得具有 25 个 NSString 对象的普通 NSArray?

4

2 回答 2

0

您正在查看 JSON 中 polyline 字段的正确输出。数据使用 Google 的折线编码进行编码,在此处记录

https://developers.google.com/maps/documentation/utilities/polylinealgorithm

网上有很多方法可以在objective-c中解析所述字符串,例如this one

于 2012-08-15T08:16:40.730 回答
0

好的,我找到了使用 NSJSONSerialization 解析 json 的解决方案:

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataRequest options:NSJSONReadingMutableContainers error: nil];
NSArray * items = [jsonArray valueForKeyPath:@"routes.legs.steps.polyline.points"];
items = [items objectAtIndex:0];
items = [items objectAtIndex:0];

在 items 数组中,您将获得带有编码方向的字符串

于 2012-08-16T18:30:45.530 回答