双对象嵌套JSON响应主体[Objective C]

时间:2015-09-03 14:53:57

标签: ios objective-c json

在Objective C中,我应该如何在表单中编写Double Nested Response Body:

yticks

我知道如何为一个对象做这件事,但是双重嵌套对象让我感到难过。

这是我到目前为止所做的:

import matplotlib.pyplot as plt
import numpy as np

data = [np.random.normal(np.random.randint(0,5),4,25) for _ in range(25)] # some random data

fig=plt.figure()
ax1=fig.add_subplot(111)
ax2=ax1.twinx()

ax1.set_ylim(-5,25)
ax2.set_ylim(0,14)
ax2.set_yticks([10,12,14]) # ticks below 10 don't show up

ax1.boxplot(data)
ax2.plot(np.linspace(0,26,50),12.+2.*np.sin(np.linspace(0,2.*np.pi,50))) # just a random line

plt.show()

2 个答案:

答案 0 :(得分:3)

您必须在另一个字典中添加字典。

NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys:
                 [loginDetails objectAtIndex:0], @"password",
                 [loginDetails objectAtIndex:1], @"username",
                 [loginDetails objectAtIndex:2], @"api_key",
                 nil];

NSDictionary *userDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                 tmp, @"user"];

NSDictionary *requestDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                 userDict, @"ms_request"];

NSData *postdata = [NSJSONSerialization dataWithJSONObject:requestDict options:0 error:&error];

PS:在JSON中, {} 代表字典, [] 代表数组。

答案 1 :(得分:3)

您也可以使用NSDictionary literals。这样的事情(只需确保检查所有值以避免插入nil个对象):

NSDictionary *reqDict = 
@{@"ms_request":
         @{@"user":
             @{@"api_key":loginDetails[2],
              @"username":loginDetails[1],
              @"password":loginDetails[0]
             }
         }
 };