有3个字符串变量
public var userLoginId : String?
public var searchString : String?
public var tableName : String?
我有一本字典:
let dict = ["userLoginId" : userLoginId, "searchString" : searchString,"tableName" : tableName]
现在我按JSON序列化它:
let data = try! JSONSerialization.data(withJSONObject: dict, options :[])
let jsonstring = String(data:data, encoding:.utf8)!
在jsonstring
中,每次我获得如下所示的无序JSON:
"{\"tableName\":\"null\",\"userLoginId\":\"Dilip\",\"searchString\":\"Tata\"}"
如何获得我为字典赋值的格式?
答案 0 :(得分:2)
Swift的
不仅没有订购,而且按照标准也没有JSON词典。您可能做的最好的事情是将键以正确的顺序存储在数组中。您不是迭代字典,而是迭代有序的键数组,然后使用这些键从字典中获取。PayPal\Api\Agreement Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[name] => Base Agreement
[description] => Basic Agreement
[start_date] => 2017-02-17T17:45:04Z
[plan] => PayPal\Api\Plan Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[payment_definitions] => Array
(
[0] => PayPal\Api\PaymentDefinition Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[type] => REGULAR
[frequency] => Month
[amount] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 100.00
)
)
[cycles] => 12
[charge_models] => Array
(
[0] => PayPal\Api\ChargeModel Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[type] => TAX
[amount] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 0.00
)
)
)
)
[1] => PayPal\Api\ChargeModel Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[type] => SHIPPING
[amount] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 10.00
)
)
)
)
)
[frequency_interval] => 2
)
)
)
[merchant_preferences] => PayPal\Api\MerchantPreferences Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[setup_fee] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 1.00
)
)
[max_fail_attempts] => 0
[auto_bill_amount] => YES
)
)
[links] => Array
(
)
[currency_code] => USD
)
)
[payer] => PayPal\Api\Payer Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[payment_method] => credit_card
[payer_info] => PayPal\Api\PayerInfo Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
)
)
[funding_instruments] => Array
(
[0] => PayPal\Api\FundingInstrument Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[credit_card] => PayPal\Api\CreditCard Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[type] => visa
[number] => 7456
[expire_month] => 06
[expire_year] => 2018
[first_name] => Edford Patrick
[last_name] => Bedia
)
)
)
)
)
)
)
[shipping_address] => PayPal\Api\ShippingAddress Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[line1] => 111 First Street
[city] => Saratoga
[state] => CA
[postal_code] => 95070
[country_code] => US
)
)
[id] => I-9107LMJ0351R
[state] => Active
[links] => Array
(
[0] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-9107LMJ0351R
[rel] => self
[method] => GET
)
)
)
[agreement_details] => PayPal\Api\AgreementDetails Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[outstanding_balance] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 0.00
)
)
[cycles_remaining] => 12
[cycles_completed] => 0
[next_billing_date] => 2017-02-17T10:00:00Z
[last_payment_date] => 2017-02-17T05:46:56Z
[last_payment_amount] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 1.00
)
)
[final_payment_date] => 2018-12-17T10:00:00Z
[failed_payment_count] => 0
)
)
)
)
为避免手动重复密钥,您可以将字典表示为PayPal\Api\Agreement Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[name] => Base Agreement
[description] => Basic Agreement
[start_date] => 2017-02-17T17:45:04Z
[plan] => PayPal\Api\Plan Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[payment_definitions] => Array
(
[0] => PayPal\Api\PaymentDefinition Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[type] => REGULAR
[frequency] => Month
[amount] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 100.00
)
)
[cycles] => 12
[charge_models] => Array
(
[0] => PayPal\Api\ChargeModel Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[type] => TAX
[amount] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 0.00
)
)
)
)
[1] => PayPal\Api\ChargeModel Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[type] => SHIPPING
[amount] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 10.00
)
)
)
)
)
[frequency_interval] => 2
)
)
)
[merchant_preferences] => PayPal\Api\MerchantPreferences Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[setup_fee] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 1.00
)
)
[max_fail_attempts] => 0
[auto_bill_amount] => YES
)
)
[links] => Array
(
)
[currency_code] => USD
)
)
[payer] => PayPal\Api\Payer Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[payment_method] => credit_card
[payer_info] => PayPal\Api\PayerInfo Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
)
)
[funding_instruments] => Array
(
[0] => PayPal\Api\FundingInstrument Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[credit_card] => PayPal\Api\CreditCard Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[type] => visa
[number] => 7456
[expire_month] => 06
[expire_year] => 2018
[first_name] => Edford Patrick
[last_name] => Bedia
)
)
)
)
)
)
)
[shipping_address] => PayPal\Api\ShippingAddress Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[line1] => 111 First Street
[city] => Saratoga
[state] => CA
[postal_code] => 95070
[country_code] => US
)
)
[id] => I-9107LMJ0351R
[state] => Active
[links] => Array
(
[0] => PayPal\Api\Links Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[href] => https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-9107LMJ0351R
[rel] => self
[method] => GET
)
)
)
[agreement_details] => PayPal\Api\AgreementDetails Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[outstanding_balance] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 0.00
)
)
[cycles_remaining] => 12
[cycles_completed] => 0
[next_billing_date] => 2017-02-17T10:00:00Z
[last_payment_date] => 2017-02-17T05:46:56Z
[last_payment_amount] => PayPal\Api\Currency Object
(
[_propMap:PayPal\Common\PayPalModel:private] => Array
(
[value] => 1.00
)
)
[final_payment_date] => 2018-12-17T10:00:00Z
[failed_payment_count] => 0
)
)
)
)
元组的数组,如下所示:
然后,您可以使用这个漂亮的Dictionary
扩展程序,从(Key, Value)
个元组中创建一个新的let keyValuePairs = [
("userLoginId", userLoginId),
("searchString", searchString),
("tableName", tableName)
]
:
Dictionary
现在您可以在Swift代码中使用Dictionary
,或者将它们与(Key, Value)
一起存储在JSON中:
extension Dictionary {
init(_ keyValuePairs: [(Key, Value)]) {
self.init(minimumCapacity: keyValuePairs.count)
for (key, value) in keyValuePairs {
self[key] = value
}
}
}
let dict = Dictionary(keyValuePairs)
let orderedKeys = keyValuePairs.map{ $0.0 }
答案 1 :(得分:1)
我知道这是在一年前被问到的。但是我最近在编写单元测试时遇到了这个问题。基本上我编写了一个模拟JSON字典,使用JSONSerialization将其转换为数据,并将其传递给各种解析对象。然而,在这个特定的测试中,必须保持顺序或测试失败,并且前面描述的方法没有这样做。那么对我有用的不是从字典开始,而是从测试JSON字符串开始,并使用utf8将其编码为数据。当它被解析为字典时,JSON字符串的顺序将保存在已解析的字典中。
答案 2 :(得分:1)
Dictionaries
无序
由于 JSON
是String
,因此您可以拥有已排序的JSON
。在 iOS 11 和 macOS 10.13 中,JSONSerialization
有一个新选项,用于对可以使用的键进行排序:
JSONSerialization.data(withJSONObject: json, options: [.sortedKeys])
答案 3 :(得分:0)
字典是“无序集合”。他们对其键/值对没有任何订单。周期。
如果您想要有序集合,请使用字典以外的其他内容。 (单项词典数组是一种方法。)您还可以编写将字典的键加载到可变数组中的代码,对数组进行排序,然后使用排序的键数组来获取键/值对。期望的顺序。
您还可以创建自己的集合类型,使用字符串作为索引,并按顺序保留项目。 Swift使这很简单,虽然它的计算成本很高。