这是最初的反应

{
    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";
}

首先,我解析“消息”。然后我得到了一个像这样的杰森:
{
    "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"])
}

无/空

最佳答案

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


  • 注意:这不会将String解析为JSON,而是使用init(parseJSON: String)

  • let message = JSON(parseJSON: json["message"].stringValue)
    

    08-27 07:23