gpt4 book ai didi

ios - Json 有值但它返回 null/nill

转载 作者:可可西里 更新时间:2023-11-01 00:54:00 27 4
gpt4 key购买 nike

这是最初的 react

{
message = "{\n \"classesToTeach\":[\n \"Primary\", \"Secondary\", \"Higher Secondary\", \"Admission\"\n ],\n \"subcategories\":{\n \"Academics\":[\n \"Cambridge\", \"Edexcel\", \"IB\", \"GRE\", \"SAT\", \"American\", \"Australian\", \"National Bangla Medium\", \"National English Version\", \"Madrasa\"\n ],\n \"Music\":[\n \"Guitar\", \"Vocal\", \"Drums\", \"Keyboard\", \"Tabla\", \"Flute\", \"Harmonium\", \"Violin\", \"Ukulele\"\n ],\n \"Languages\":[\n \"English\", \"Bangla\", \"French\", \"Spanish\", \"German\", \"Chinese\", \"Arabic\", \"Hindi\"\n ],\n \"IT\":[\n \"Programming\", \"SEO\", \"Graphics Designing\", \"Web Development\", \"Video Editing\"\n ],\n \"Cooking\":[\n \"Bangla\", \"Indian\", \"Thai\", \"Chinese\", \"Continental\", \"Baking\"\n ],\n \"Sports\":[\n \"Football\", \"Basketball\", \"Cricket\", \"Lawn tennis\", \"Table tennis\", \"Archery\", \"Chess\", \"Badminton\", \"Swimming\", \"Cycling\", \"Billiard\", \"Fitness\"\n ],\n \"Fine Arts\":[\n \"Drawing and Painting\", \"Sculpture\"\n ],\n \"Driving\":[\n \"Car\", \"Motorcycle\"\n ],\n \"Religion\":[\n \"Islam\", \"Christianity\", \"Buddhism\", \"Hinduism\"\n ],\n \"Dancing\":[\n \"Classical\", \"Salsa\", \"Hiphop\", \"Tango\"\n ]\n },\n \"subjects\":[\n \"English\", \"English Literature\", \"Bangla\", \"General Science\", \"Mathematics\", \"Further/Higher Mathematics\",\n \"Physics\", \"Chemistry\", \"Biology\", \"Economics\", \"Accounting\", \"Business Studies\", \"Commerce\", \"Geography\",\n \"History\", \"ICT\", \"Social Science\", \"Religious Studies\", \"Bangladesh and Global Studies\"\n ]\n}\n";
}

首先,我解析“消息”。然后我得到了一个这样的 Json:

{
"classesToTeach":[
"Primary", "Secondary", "Higher Secondary", "Admission"
],
"subcategories":{
"Academics":[
"Cambridge", "Edexcel", "IB", "GRE", "SAT", "American", "Australian", "National Bangla Medium", "National English Version", "Madrasa"
],
"Music":[
"Guitar", "Vocal", "Drums", "Keyboard", "Tabla", "Flute", "Harmonium", "Violin", "Ukulele"
],
"Languages":[
"English", "Bangla", "French", "Spanish", "German", "Chinese", "Arabic", "Hindi"
],
"IT":[
"Programming", "SEO", "Graphics Designing", "Web Development", "Video Editing"
],
"Cooking":[
"Bangla", "Indian", "Thai", "Chinese", "Continental", "Baking"
],
"Sports":[
"Football", "Basketball", "Cricket", "Lawn tennis", "Table tennis", "Archery", "Chess", "Badminton", "Swimming", "Cycling", "Billiard", "Fitness"
],
"Fine Arts":[
"Drawing and Painting", "Sculpture"
],
"Driving":[
"Car", "Motorcycle"
],
"Religion":[
"Islam", "Christianity", "Buddhism", "Hinduism"
],
"Dancing":[
"Classical", "Salsa", "Hiphop", "Tango"
]
},
"subjects":[
"English", "English Literature", "Bangla", "General Science", "Mathematics", "Further/Higher Mathematics",
"Physics", "Chemistry", "Biology", "Economics", "Accounting", "Business Studies", "Commerce", "Geography",
"History", "ICT", "Social Science", "Religious Studies", "Bangladesh and Global Studies"
]
}

我尝试使用 SwiftyJSON 访问 classesToTeach 或其他属性。它返回零/空。你们能帮帮我吗????

Alamofire.request(url, method: .get, parameters: nil,encoding: JSONEncoding.default, headers: headers)
.responseJSON { response in

let json = JSON(response.result.value!);
let message = JSON(json["message"])

print(json["message"])
print(message["classesToTeach"])
}

无/空

最佳答案

阅读 SwiftyJSON 的文档是值得的。在 init(_ object: Any) 的描述中有一个注释:

  • note: this does not parse a String into JSON, instead use init(parseJSON: String)
let message = JSON(parseJSON: json["message"].stringValue)

关于ios - Json 有值但它返回 null/nill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57253340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com