我将如何与没有钥匙的JSON合作:使用urlsession在Swift中的价值对?

发布于 2025-02-10 16:01:32 字数 506 浏览 2 评论 0原文

我正在尝试在返回的JSON中访问每个标题。这是JSON

[
    "Hyouka",
    "Youjo Senki",
    "Bungou Stray Dogs 2nd Season",
    "Fullmetal Alchemist: Brotherhood",
    "Tokyo Ghoul √A",
    "Mahouka Koukou no Rettousei",
    "Boku wa Tomodachi ga Sukunai NEXT",
    "Joker Game",
    "Avatar: The Last Airbender",
    "Charlotte"
]

,这只是一堆值,我没有钥匙来构建我的模型对象。这就是我打算这样做的方式,

struct AllNames {
    
    let name: String   
}

但是没有关键名称可以访问。您将如何访问此数据将每个名称打印到Swift中的控制台?

I am trying to access each title in a returned json. This is the JSON

[
    "Hyouka",
    "Youjo Senki",
    "Bungou Stray Dogs 2nd Season",
    "Fullmetal Alchemist: Brotherhood",
    "Tokyo Ghoul √A",
    "Mahouka Koukou no Rettousei",
    "Boku wa Tomodachi ga Sukunai NEXT",
    "Joker Game",
    "Avatar: The Last Airbender",
    "Charlotte"
]

It's just a bunch of values and no key for me to construct my model object. This is how I would plan to do it

struct AllNames {
    
    let name: String   
}

But there's no key name for me to access. How would you go about accessing this data to print each name to the console in Swift?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2025-02-17 16:01:32

您的JSON是一系列字符串,因此没有模型,您只需要

do {
    let arr = try JSONDecoder().decode([String].self, from: jsonData)
    print(arr)
 }
 catch {
    print(error)
 }

Your json is an array of strings , so no model is here and you only need

do {
    let arr = try JSONDecoder().decode([String].self, from: jsonData)
    print(arr)
 }
 catch {
    print(error)
 }
许久 2025-02-17 16:01:32

将JSON字符串转换为数组:

    func getArrayFromJSONString(jsonStr: String) -> Array<Any> {
        
        let jsonData: Data = jsonStr.data(using: .utf8)!
     
        let arr = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        
        if arr != nil {
            return arr as! Array<Any>
        }
        
        return []
    }

测试案例:

    let jsonStr = """
    [
        \"Hyouka\",
        \"Youjo Senki\",
        \"Bungou Stray Dogs 2nd Season\",
        \"Fullmetal Alchemist: Brotherhood\",
        \"Tokyo Ghoul √A\",
        \"Mahouka Koukou no Rettousei\",
        \"Boku wa Tomodachi ga Sukunai NEXT\",
        \"Joker Game\",
        \"Avatar: The Last Airbender\",
        \"Charlotte\"
    ]
    """
    
    let arr = getArrayFromJSONString(jsonStr: jsonStr)
    
    print(arr)

打印日志:

[Hyouka, Youjo Senki, Bungou Stray Dogs 2nd Season, Fullmetal Alchemist: Brotherhood, Tokyo Ghoul √A, Mahouka Koukou no Rettousei, Boku wa Tomodachi ga Sukunai NEXT, Joker Game, Avatar: The Last Airbender, Charlotte]

Convert JSON string to array:

    func getArrayFromJSONString(jsonStr: String) -> Array<Any> {
        
        let jsonData: Data = jsonStr.data(using: .utf8)!
     
        let arr = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        
        if arr != nil {
            return arr as! Array<Any>
        }
        
        return []
    }

Test case:

    let jsonStr = """
    [
        \"Hyouka\",
        \"Youjo Senki\",
        \"Bungou Stray Dogs 2nd Season\",
        \"Fullmetal Alchemist: Brotherhood\",
        \"Tokyo Ghoul √A\",
        \"Mahouka Koukou no Rettousei\",
        \"Boku wa Tomodachi ga Sukunai NEXT\",
        \"Joker Game\",
        \"Avatar: The Last Airbender\",
        \"Charlotte\"
    ]
    """
    
    let arr = getArrayFromJSONString(jsonStr: jsonStr)
    
    print(arr)

Print log:

[Hyouka, Youjo Senki, Bungou Stray Dogs 2nd Season, Fullmetal Alchemist: Brotherhood, Tokyo Ghoul √A, Mahouka Koukou no Rettousei, Boku wa Tomodachi ga Sukunai NEXT, Joker Game, Avatar: The Last Airbender, Charlotte]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文