Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何为对象创建具有空键/无键的NSDictionary_Ios_Objective C_Nsdictionary - Fatal编程技术网

Ios 如何为对象创建具有空键/无键的NSDictionary

Ios 如何为对象创建具有空键/无键的NSDictionary,ios,objective-c,nsdictionary,Ios,Objective C,Nsdictionary,我想从NSDictionary创建一个JSON字符串 { "source":"point a ", "destination":"point b ", "boardingPoint":{ "id":"2222", "location":"Some location" }, "customerName":"test", "customerLastName":"testing", "customerEmail":"test@g

我想从
NSDictionary
创建一个JSON字符串

{
     "source":"point a ",
     "destination":"point b ",
     "boardingPoint":{
           "id":"2222",
           "location":"Some location"
},
"customerName":"test",
"customerLastName":"testing",
"customerEmail":"test@gmail.com",
"blockSeatPaxDetails":[
  {
     "age":"20",
     "name":"test123",
     "sex":"M",
     "title":"Mr",
     "email":"testing@gmail.com"
  }
],
"inventory":0
}
现在我一直在为这个JSON ie的第一部分创建字典

    {
    "source": "point a ",
    "destination": "point b ",
    "boardingPoint": {
    "id": "2222",
    "location": "Some location",

   },
这里的boardingPoint是一个添加到一个字典中的字典。然后与其余的键一起创建一个最终字典。因此,当我创建最终字典时,使用
对象和键
我没有要为下面显示的第一部分设置的键。如果我像这样设置为空
@“
JSON字符串将显示
作为键。当然我不能使用
nil
。那怎么做呢

如果我在第一部分中插入空格作为键,就会出现这种情况

  {
  "" : {

    "source" : "point a",
    "boardingPoint" : {
    "id" : "2222",
    "location" : "Some location",
  },
    "destination" : "point b"
 },
这就是我创建字典的方式

  NSDictionary *boardingPoint = [NSDictionary dictionaryWithObjectsAndKeys:boardingPointID,@"id", boardingLocation,@"location",nil];

  NSDictionary *firstDictionary = [NSDictionary dictionaryWithObjectsAndKeys:source,@"source",dest,@"destination",nil];

 NSDictionary *mainDictionary = [NSDictionary dictionaryWithObjectsAndKeys:firstDictionary, @" ",customerName,@"customerName",customerLastName,@"customerLastName",customerEmail,@"customerEmail",blockSeatPaxDetails,@"blockSeatPaxDetails",inventory,@"inventory",nil];
然后当然

     NSData *finalData = [NSJSONSerialization dataWithJSONObject:mainDictionary options:NSJSONWritingPrettyPrinted error:nil];

正确缩进的JSON的第一部分是:

{

    "source":"point a ",
    "destination":"point b ",
    "boardingPoint":{
        "id":"2222",
        "location":"Some location",
    },
    "customerName":"test",
    "customerLastName":"testing",
    "customerEmail":"test@gmail.com",
    "blockSeatPaxDetails":[
        {
            "age":"20",
            "name":"test123",
            "sex":"M",
            "title":"Mr",
            "email":"testing@gmail.com",
        },
(很明显,你在结尾处省略了一些内容,所以我无法放慢最后几行的速度。)


没有“丢失的钥匙”或类似的东西。如果您通过NSJSONSerialization为您的(未滑动的)JSON提供数据,它将生成正确的字典和数组“树”。

您完全弄错了缩进。是的,我知道。将其编辑到目前为止的最佳状态。[NSNull Null Null]对于NSJSONSerializations或NSArrays中的nil,您在“源代码”之前的第二个
{/code>?它不在您的第一个列表中。不要使用带有Objects和Keys的
字典,因为它太冗长和混乱。或者创建一个NSMutableDictionary并对每个项目分别执行
setObject:forKey:
,或者使用
@{key1:object1,key2:object2}
符号。