如何将rawString转换为JSON数组?

问题描述 投票:-3回答:1

我试图将jsonString.rawString()转换为JSON对象,但它返回nil。

let jsonData = newsfeedData.rawString()?.data(using: .utf8)
let object = try? JSONSerialization.jsonObject(with: jsonData!, options: .mutableContainers)
print(object) //it returns nil

这是jsonString:

打印newsfeedData.rawString的描述:

"Data:[{\"UserID\":1,\"post\":[{\"PostId\":1,\"UserId\":1,\"UserName\":\"party Patel\",\"ImagePath\":\"/Files/User/user_20180606_040913967_Hydrangeas.jpg\",\"FileId\":2108,\"FileName\":\"Business Category.png\",\"Email\":\"[email protected]\",\"Location\":\"Ahmedabad\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Photo/fd1c7fcf-e1d0-4ffe-afb4-42bb3fea4fa4.PNG\",\"UserRole\":\"Business\",\"BusinessName\":\"Shaligram infotech\",\"PostType\":false,\"Type\":\"Photo\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":4,\"CommentCount\":4,\"ViewCount\":3},{\"PostId\":3,\"UserId\":1,\"UserName\":\"party Patel\",\"ImagePath\":\"/Files/User/user_20180606_040913967_Hydrangeas.jpg\",\"FileId\":2110,\"FileName\":\"DiscovrUS.mov\",\"Email\":\"[email protected]\",\"Location\":\"Ahmedabad\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Video/ab48c228-d7b5-4dff-af56-31a0ac159a35.MOV\",\"UserRole\":\"Business\",\"BusinessName\":\"Shaligram infotech\",\"PostType\":true,\"Type\":\"Video\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":1,\"CommentCount\":0,\"ViewCount\":1}]},{\"UserID\":2,\"post\":[{\"PostId\":2,\"UserId\":2,\"UserName\":\"tejas Padia\",\"ImagePath\":\"/Files/User/user_20180606_062946997_Tulips.jpg\",\"FileId\":2109,\"FileName\":\"DiscovrUS.mov\",\"Email\":\"[email protected]\",\"Location\":\"India\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Video/2d892eaf-b6dc-433d-985b-2a4588ffd307.MOV\",\"UserRole\":\"Individual\",\"BusinessName\":\"\",\"PostType\":true,\"Type\":\"Video\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":3,\"CommentCount\":0,\"ViewCount\":0}]}]"
ios json swift
1个回答
1
投票

确保json格式化您的值无效格式只需尝试将Data:替换为空字符串"",您的代码正在工作

  do{


        var  json = "Data:[{\"UserID\":1,\"post\":[{\"PostId\":1,\"UserId\":1,\"UserName\":\"party Patel\",\"ImagePath\":\"/Files/User/user_20180606_040913967_Hydrangeas.jpg\",\"FileId\":2108,\"FileName\":\"Business Category.png\",\"Email\":\"[email protected]\",\"Location\":\"Ahmedabad\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Photo/fd1c7fcf-e1d0-4ffe-afb4-42bb3fea4fa4.PNG\",\"UserRole\":\"Business\",\"BusinessName\":\"Shaligram infotech\",\"PostType\":false,\"Type\":\"Photo\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":4,\"CommentCount\":4,\"ViewCount\":3},{\"PostId\":3,\"UserId\":1,\"UserName\":\"party Patel\",\"ImagePath\":\"/Files/User/user_20180606_040913967_Hydrangeas.jpg\",\"FileId\":2110,\"FileName\":\"DiscovrUS.mov\",\"Email\":\"[email protected]\",\"Location\":\"Ahmedabad\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Video/ab48c228-d7b5-4dff-af56-31a0ac159a35.MOV\",\"UserRole\":\"Business\",\"BusinessName\":\"Shaligram infotech\",\"PostType\":true,\"Type\":\"Video\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":1,\"CommentCount\":0,\"ViewCount\":1}]},{\"UserID\":2,\"post\":[{\"PostId\":2,\"UserId\":2,\"UserName\":\"tejas Padia\",\"ImagePath\":\"/Files/User/user_20180606_062946997_Tulips.jpg\",\"FileId\":2109,\"FileName\":\"DiscovrUS.mov\",\"Email\":\"[email protected]\",\"Location\":\"India\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Video/2d892eaf-b6dc-433d-985b-2a4588ffd307.MOV\",\"UserRole\":\"Individual\",\"BusinessName\":\"\",\"PostType\":true,\"Type\":\"Video\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":3,\"CommentCount\":0,\"ViewCount\":0}]}]"

        json = json.replacingOccurrences(of: "Data:", with: "")
        if let data = json.data(using: .utf8) {
            do {
                let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]]
                print(jsonResult)
            } catch {
                print(error.localizedDescription)
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.