Swift:如何将收到的字符串转换为json

时间:2015-02-03 13:43:12

标签: json swift

我有以下问题。 我使用play2从服务器接收jsonString。在swift中,我将这个jsonString转换为jsonDictionary。如果我收到的jsonString只包含一个jsonDocument,我可以像下面的代码一样转换它。

recieved Data:
({"_id":{"$oid":"54cfc217dd99a083af6c3a54"},"kind":"photo","username":"gokn2","article":"das ist ein Test2","datetime":"31.12.2015 22:22:10","city":"Berlin","latitude":"52.00","longitude":"12.02","startdate":"12:00:00","enddate":"12:00:00","binary":"iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAAKAAAAKAAAAAoAAAAKAAACm4NU0j0AAAJnSURBVDgRbJLdS1NxGMdXl9IfEUQ3XdVNRBAF3UaCWEYvFAgiCV6kJJrURGuI5WtTpjCXL9lM3atzmzo1Z5tbO2XuLbep29nbOWfnOGka1MW3fr+gBP3Bw+/HA8/n+X5/zyOTHXH07kzR+1W+zOzjRu0r8djiXKhgtwUL6kl/tFrFjF1pdJSdvD1+4ojSwylPdLeECfGxxLwPQtdbSG1vID3thVjRgt36bkgKNZiOSTSpXFtnK6dLDxMOZMTvP1uzkSQkq5NGpvolhLEZSN51ZCoVEJ0+kBxpUmjqw1y3EZcf29oPIP4/9378ai1kOAjjdgg6B3a2WQiGRYgaA/KDBoiP2sHJVcjU9VBw4mI58iU1YNQzh6HEZo7lqRJeY0S2bQiScRH5ETO2BvTwqKbh6rdgo3kQQm0nVUvARDlRam3X4fR90w0qz7TKF335JiQl8xLtTNSJr98hqTFhzPwVdcNrKO/xoKrfh5apIGYdQWzXdiP9oAn88DQS5+5BulSOJ0pnlg5qysvdZFfW6QAE8wcQcFq3gD5LGPIhBs1DPqiMARidUdjcm2CiHNKFfQj1PWCvPkT2hZoOyiXX4EyV8Y6MrMbOxCwkJghil0zUMB+CdiECy8dNuP0sYn++I54SEIlz+BRKYWo5BsusH+nrNcjZXeCVWnAVz1HaPD8hI3uWa+wF1zFKLRB4gBUphM3k6L22kaYQpd6P+gEvrjXM4VaHB4EGFUhtsrj2r+0uR0JmsoX3yI6RJOmYMi0jmS/gczhFbaqtYQq5q1hCiXzhXzRoA7A9G0Hq2HlsHb+A3KlivOqy7/8GAAD//5+9LFkAAAJkSURBVHWSS0wTYRDHN5z17tHoRT0YuZF4UA4a44uEoGB5+EiIGKogNpUWpDyUCk1bWFyVGpCgQksI1EYpFNwG3FiV2hLCo4bGFWhh26UNhcr1b7/PYMDETWZ3vnwzv/nPzDJW+/RWXKFF+NxdrJ4uhccyhFcTIjQvJnGb/YQC/ThyG9y42jSB2m4fWgdn4BCCCEQ3EOAGIKZlQNybibWDWTC2urYY1jr1gwCX0gsp8HvzG5R1+imk7IkHja994Owz4L0/4Q+EsbAYoTYnbyJeb4G47yyFxtMVqGrll5g77b5eWdUC6X4bYsI3REofY5SfQ+dwgEK882EshmVqvpRvcy9QlVyPF7EzSkTarDRXSom63Dg2yJys5nO9nB3xmqdYc3kgNXbSyqH1JGaXY/g4tURbJCqLjQIu1I1D2eFH8MZDyJqUCM80jf9ssOLwTUchs1/Rt6eKE6REcT2iHfY/FYt0ECtaMPllAVrbLPLMX5FR6UZmnQAdJyCoNGKtksXyKSVCWSokruugNo3JhMWQ51jJ+xzXy1GQOaxcq4Nk6MZqiR7S+XLMl7MY03ZRIz5pU0oVlHudiHY5aI7TOIBDt97mUdj264R6xDTZ1INNtZmqDBXUQO5z0ZnK2SrEKkxYuXgPkef9kN9NUHUbZc0g4zqudrLbnF1fAnVanFjPTiUevYIoZ0N8WIBU/QyR2nb6JxCfFEgUPcCQ3kZhB/JtabtAOw9EusrMh8iQycYTGpa2lTR3gygi2yR3JOZIqSN/Z+5/fTJcEpzT8KFf0zwiGkwjSYPe8Yv4lx7x/WSbfxfwD+U3jf3fV9sI+agAAAAASUVORK5CYII="})

Swift中的代码:

let data=self.recvJsonString?.dataUsingEncoding(NSUTF8StringEncoding)
var jsonDict=NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers, error: &error) as NSDictionary

但是如果json字符串包含多个json文档,它就不起作用:

("{\"_id\":{\"$oid\":\"54cfd810dd99a083af6c3a5b\"},\"kind\":\"article\",\"username\":\"gokn1\",\"article\":\"das ist ein Test1\",\"datetime\":\"31.12.2015 22:22:10\",\"city\":\"Berlin\",\"latitude\":\"51.00\",\"longitude\":\"11.00\",\"startdate\":\"11:00:00\",\"enddate\":\"11:00:00\",\"binary\":\"\"}",
"{\"_id\":{\"$oid\":\"54cfc217dd99a083af6c3a54\"},\"kind\":\"photo\",\"username\":\"gokn2\",\"article\":\"das ist ein Test2\",\"datetime\":\"31.12.2015 22:22:10\",\"city\":\"Berlin\",\"latitude\":\"52.00\",\"longitude\":\"12.02\",\"startdate\":\"12:00:00\",\"enddate\":\"12:00:00\",\"binary\":\"iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAAKAAAAKAAAAAoAAAAKAAACm4NU0j0AAAJnSURBVDgRbJLdS1NxGMdXl9IfEUQ3XdVNRBAF3UaCWEYvFAgiCV6kJJrURGuI5WtTpjCXL9lM3atzmzo1Z5tbO2XuLbep29nbOWfnOGka1MW3fr+gBP3Bw+/HA8/n+X5/zyOTHXH07kzR+1W+zOzjRu0r8djiXKhgtwUL6kl/tFrFjF1pdJSdvD1+4ojSwylPdLeECfGxxLwPQtdbSG1vID3thVjRgt36bkgKNZiOSTSpXFtnK6dLDxMOZMTvP1uzkSQkq5NGpvolhLEZSN51ZCoVEJ0+kBxpUmjqw1y3EZcf29oPIP4/9378ai1kOAjjdgg6B3a2WQiGRYgaA/KDBoiP2sHJVcjU9VBw4mI58iU1YNQzh6HEZo7lqRJeY0S2bQiScRH5ETO2BvTwqKbh6rdgo3kQQm0nVUvARDlRam3X4fR90w0qz7TKF335JiQl8xLtTNSJr98hqTFhzPwVdcNrKO/xoKrfh5apIGYdQWzXdiP9oAn88DQS5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ycYTGpa2lTR3gygi2yR3JOZIqSN/Z+5/fTJcEpzT8KFf0zwiGkwjSYPe8Yv4lx7x/WSbfxfwD+U3jf3fV9sI+agAAAAASUVORK5CYII=\"}"

我似乎必须首先拆分jsons。但是我该怎么做呢 任何人都可以帮我解决这个问题。

由于

1 个答案:

答案 0 :(得分:1)

第二个示例不是有效的JSON字符串。尝试使用jsonlint.com来验证JSON字符串。仔细看......这些实际上是两个字符串。

双引号标记字符串的边界。

"{ \"val1\" : \"string1" }", "{ \"val1\" : \"String2\" }"

所以...将其更改为以下以使其成为单个JSON字符串。

"{ \"val1\" : \"string1" }, { \"val1\" : \"String2\" }"