Json 有值,但它返回 null/nill

Posted

技术标签:

【中文标题】Json 有值,但它返回 null/nill【英文标题】:Json has value but it returns null/nill 【发布时间】:2019-07-29 11:54:39 【问题描述】:

这是最初的回应


    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 或其他属性。它返回 nil/null。你们能帮帮我吗????

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"])     

无/空

【问题讨论】:

【参考方案1】:

阅读SwiftyJSON 的文档是值得的。在init(_ object: Any)的描述中有一条说明:

注意:这不会将String 解析为JSON,而是使用init(parseJSON: String)
let message = JSON(parseJSON: json["message"].stringValue)

【讨论】:

json 已经是一个 SwiftyJSON 对象。所以现在你正在从字符串转换为 SwiftyJSON 再转换回字符串并再次转换回 SwiftyJSON。 @AaronBrager 键 message 的值是另一个(嵌套)JSON 字符串,必须在 第二级 进行反序列化 @vadian 啊,我明白了。感谢您的澄清

以上是关于Json 有值,但它返回 null/nill的主要内容,如果未能解决你的问题,请参考以下文章

当实际有值时,响应正文返回 null

返回 Json,但它包含我不想要的反斜杠“\”

WCF 休息服务不返回 JSON 数据。但它适用于 XML

sql语句在实际有值时返回null

为啥 Oracle Left Join 在应该有值的地方返回 null

使用LoadRunner发送json格式登陆参数后怎样获取登陆验证的token