如何从Swift4中的嵌套字典访问值?

时间:2018-12-24 08:48:13

标签: ios json iphone swift dictionary

我想在json结构中获取 City:的值。我在带有字典的字典中接收到数据:[String:任何]?

我已经尝试了几种方法,但对我没有任何帮助。我认为我的问题是因为对包含多个{和[

{
"Format": "XXXXXXXXX",
"FormatVersion": "1.0",
"Status": "OK",
"NumToReturn": 1,
"AllResults": [
{
  "ConversationState": {
    "ConversationStateTime": XXXXXXXXX,
    "QueryEntities": {
      "Where": [
        {
          "Type": "City",
          "Label": "Stuttgart, Germany",
          "SpokenLabel": "Stuttgart",
          "Address": "Stuttgart, Baden-Wurttemberg, Germany",
          "City": "Stuttgart",
          "Admin2": "Regierungsbezirk Stuttgart",
          "Admin1": "Baden-Wurttemberg",
          "Country": "Germany",
          "CountryCode": "DE",
          "IATA": "STR",
          "Geohash": "u0wt8bd9854n",
          "Verified": true,
          "HighConfidence": true,
          "CurrentLocation": false,
          "Latitude": 48.78231811523438,
          "Longitude": 9.177020072937012,
          "ReferenceDatum": "WGS84",
          "TimeZone": "Europe/Berlin",
          "Radius": 10,
          "Links": [
            {
              "Label": "Wikipedia",
              "URL": "http://en.wikipedia.org/wiki/Stuttgart"
            }
          ],
          "TypeID": 5,
          "SourceID": 2,
          "RecordID": 2825297
        }
      ]
    }
   }
  }
 ]
}

3 个答案:

答案 0 :(得分:1)

<style>
* {
    box-sizing: border-box;
}
@page {
    size: 21cm 29.7cm;
    margin: 30mm 45mm 30mm 45mm;
     /* change the margins as you want them to be. */
}
.A4 {
  background: white;
  width: 26cm;

  display: block;
  margin: 0 auto;
  padding: 10px 25px;
  margin-bottom: 0.5cm;
  box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.5);

  box-sizing: border-box;
}



@media print {
  body {
    margin: 0;
    padding: 0;
  }


  .A4 {
    box-shadow: none;
    margin: 0;
    width: auto;
    height: auto;
  }

  .noprint {
    display: none;
  }

  .enable-print {
    display: block;
  }
}
/* Create two equal columns that floats next to each other */
.column {
    float: left;
    width: 50%;
    padding: 30px;
    height: 300px; /* Should be removed. Only for demonstration */
}

/* Clear floats after the columns */
.row:after {
    content: "";
    display: table;
    clear: both;
}
/*
img {
   max-width:100%;
   height:auto;
   max-height:100;
}  */

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>

答案 1 :(得分:0)

如果您的Teacher.findOne({"email":req.body.email}, (err, teacher)=>{ if( !err ) { let studentEmailIds = []; teacher.students.forEach( (students) => { studentEmailIds.push(students.email); }); Student.find({'email': {"$in": studentEmailIds } }, (err, students)=>{ if(err){ //error handle }else{ console.log(students) //res.json(students); } }); } }) 不是示例,而您正在使用它,则JSON中存在无效的JSON值。除非它是整数值,否则其值应在双引号内。

ConversationStateTime

希望获得帮助!

答案 2 :(得分:0)

  1. 请检查您的JSON。 “ ConversationStateTime”的值不正确
  2. 如果有效,请尝试以下代码从JSON中获取“城市”值

    // response is your JSON
    
     func parseJSON() {
        let allResults = response!["AllResults"] as? NSArray
    
        for (_, element) in (allResults?.enumerated())! {
            let elementDict = element as? Dictionary<String, AnyObject>
            let conversationStateDict = elementDict!["ConversationState"] as? Dictionary<String, AnyObject>
            let queryDict = conversationStateDict!["QueryEntities"] as? Dictionary<String, AnyObject>
            let whereDict = queryDict!["Where"] as? NSArray
    
            for (_, whereDictElement) in (whereDict?.enumerated())! {
                let dict = whereDictElement as? Dictionary<String, AnyObject>
                print(dict!["City"])
            }
        }
    }