从String转换CLLocation对象Array

时间:2014-10-20 14:17:34

标签: ios json cllocation

嗨大家我试图将下面的JSON格式解析为CLLocation。

 {
        "id": 6,
        "userId": 62,
        "name": "town run",
        **"locations": "(\n    \"<+32.09230720,+74.17861462> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:22 PM Pakistan Standard Time\",\n    \"<+32.09231628,+74.17877018> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:24 PM Pakistan Standard Time\",\n    \"<+32.09231628,+74.17889893> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:27 PM Pakistan Standard Time\",\n    \"<+32.09231628,+74.17915642> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:32 PM Pakistan Standard Time\",\n    \"<+32.09232083,+74.17967141> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/14, 6:38:34 PM Pakistan Standard Time\"\n)"**
 }

我想将“locations”解析为Array,然后解析CLLocation。请指导我如何解决此问题。

提前致谢

1 个答案:

答案 0 :(得分:1)

您是否可以控制首先创建此JSON?如果是这样,那么最好的解决方案就是改变你创建JSON的方式。此处的“位置”是一个字符串,旨在显示为文本(即在网站或其他内容中)。

你最好将地点放在像这样的字典数组中......

"locations" : [
    {
        "lat" : 32.09230720,
        "long" : 74.17861462,
        "accuracy" : 5.0,
        "speed" : -1.0,
        "course" : -1.0,
        "date" : "10/14/14, 6:38:22 PM Pakistan Standard Time"
    },
    {
        "lat" : 32.09230720,
        "long" : 74.17861462,
        "accuracy" : 5.0,
        "speed" : -1.0,
        "course" : -1.0,
        "date" : "10/14/14, 6:38:22 PM Pakistan Standard Time"
    },
    {
        "lat" : 32.09230720,
        "long" : 74.17861462,
        "accuracy" : 5.0,
        "speed" : -1.0,
        "course" : -1.0,
        "date" : "10/14/14, 6:38:22 PM Pakistan Standard Time"
    }
]

一旦你有了这个,你就可以很容易地转换为CLLocations。

基本上,虽然您必须解析字符串并找到每个单独的部分。

你可以做点像......

NSArray *locations = [locationsString componentsSeparatedByString:@", "];

但是由于新的线条和东西,这不会起作用。