JSON响应帮助抓住每一对

时间:2018-03-29 07:06:12

标签: json swift parsing

JSON响应

class Client{
  public static void main(String[] args){
    Context ctx = getInitialContext();
    RemoteEJB ejb = (RemoteEJB) ctx.lookup(...);
    Object result = ejb.performTask(new CustomClass());
  }
}

HTTP请求

["Tidex": {
    AE =     (
        ETH,
        BTC
    );
    AGI =     (
        ETH,
        USDT,
        BTC
    );
    AHT =     (
        BTC,
        WAVES
    );
}]

我不认识' ='和';'并不确定如何抓取数据,以便我可以按照自己的意愿进行组织。我想做的是将数据组织成对,如下所示

  

AE / ETH AE / BTC

     

AGI / ETH AGI / USDT AGI / BTC

     

AHT / BTC AHT / WAVES

1 个答案:

答案 0 :(得分:1)

我建议将带有Decodable的JSON解码为包含字符串数组值的字典的伞形结构

struct Root : Decodable {

    private enum CodingKeys : String, CodingKey { case tidex = "Tidex"

    let tidex : [String:[String]]
}
...

do {
    //create json object from data
    let root = try decoder.decode(Root.self, from: data)
    for (key, value) in root.tidex {
        print("key", key, "value", value)
    }

} catch {
     print(error.localizedDescription)
}

并且 - 一如既往 - .mutableContainers在Swift中毫无意义。