我收到MY ajax的回复如下
"{\"analgesicTax\":false,\"unitPriceRequired\":false,\"toConsumer\":true,\"toRetailer\":true,\"taxName\":\"1\"}"
调用JSON.parse(result)
后,结果如下所示
{"analgesicTax":false, "unitPriceRequired":false, "toConsumer":true, "toRetailer":true, "taxName":"1"}
直到那一刻,一切似乎都没问题。但是,当我尝试使用以下代码
打印我的键/值对时 var data = JSON.parse(result);
console.log(data);
console.log(data.analgesicTax);
for (var prop in data) {
console.log(prop+"is" + data[prop]);
我尝试使用有和没有JSON.parse()
但输出如下。
0is{
1is"
2isa
3isn
4isa
5isl
6isg
7ise
8iss
9isi
10isc
11isT
12isa
13isx
14is"
15is:
16isf
17isa
18isl
19iss
20ise
21is,
22is"
23isu
24isn
25isi
26ist
27isP
28isr
29isi
30isc
31ise
32isR
33ise
34isq
35isu
36isi
37isr
38ise
39isd
40is"
41is:
42isf
43isa
44isl
45iss
46ise
47is,
48is"
49ist
50iso
51isC
52iso
53isn
54iss
55isu
56ism
57ise
58isr
59is"
60is:
61ist
62isr
63isu
64ise
65is,
66is"
67ist
68iso
69isR
70ise
71ist
72isa
73isi
74isl
75ise
76isr
77is"
78is:
79ist
80isr
81isu
82ise
83is,
84is"
85ist
86isa
87isx
88isN
89isa
90ism
91ise
92is"
93is:
94is"
95is1
96is"
97is}
在我看来,它不是像JSON对象那样对待它,而是作为字符串数组处理。任何输入?
答案 0 :(得分:1)
如果result
已经是JSON,那么您不想使用JSON.parse()
,因为JSON.parse()
会将JSON的字符串表示转换为对象。只需在编码时运行for循环,就可以获得所有键及其值。
但是,如果你开始使用JSON对象的字符串表示,那么当你想使用JSON.parse()
时就是这样。
//Starting with a JSON object
var result = {
"analgesicTax": false,
"unitPriceRequired": false,
"toConsumer": true,
"toRetailer": true,
"taxName": "1"
}
for (var prop in result) {
console.log(prop + " is " + result[prop]);
}
console.log('');
//Starting with JSON represented as a string
var result = "{\"analgesicTax\":false,\"unitPriceRequired\":false,\"toConsumer\":true,\"toRetailer\":true,\"taxName\":\"1\"}";
var data = JSON.parse(result);
for (var prop in data) {
console.log(prop + " is " + data[prop]);
}

答案 1 :(得分:0)
试试这个。
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)actionLogin:(id)sender {
[self loginForGettingAccessandRefreshToken];
}
-(void)loginForGettingAccessandRefreshToken
{
@try {
NSString *strUserName = [textUsername.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *strPassword = [textPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *strParameters = [NSString stringWithFormat:@"client_id=roclient&client_secret=secret&grant_type=password&username=%@&password=%@&scope=dataEventRecords offline_access", strUserName, strPassword];
id param = strParameters;
NSLog(@"The param is - %@",param);
[GlobalShareClass postLoginDataToServer:BASEURL_LOGIN passParameters:param success:^(id res)
{
if([res isKindOfClass:[NSDictionary class]]){
NSDictionary *dictRes = [res copy];
NSString *strErrDesc = [NSString stringWithFormat:@"%@",[dictRes objectForKey:@"error_description"]];
if([[dictRes allKeys] containsObject:@"error_description"]){
dispatch_async(dispatch_get_main_queue(), ^{
//Dispaly UI
});
}
else{
NSString *strAccessToken = [dictRes objectForKey:@"access_token"];
NSString *strRefreshToken = [dictRes objectForKey:@"refresh_token"];
NSString *strTokenType = [dictRes objectForKey:@"token_type"];
NSString *sstrExpiresIn = [dictRes objectForKey:@"expires_in"];
[[NSUserDefaults standardUserDefaults] setObject:globalShare.strRefreshToken forKey:@"refresh_token"];
[[NSUserDefaults standardUserDefaults] setObject:globalShare.strAccessToken forKey:@"access_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSDictionary *responseDict = [GlobalShareClass getDataFromToken:globalShare.strAccessToken];
NSString *strFetchedSub = [[NSUserDefaults standardUserDefaults] stringForKey:@"loginIdSub"];
globalShare.loginId = [responseDict objectForKey:@"sub"];
}
}
else{
NSLog(@"The res starts with array");
}
} failure:^(NSError *error) {
// error handling here ...
NSLog(@"%@", [error localizedDescription]);
dispatch_async(dispatch_get_main_queue(), ^{
//Showing Error
});
} passViewController:self];
}
@catch(NSException *exception){
NSLog(@"%@",[exception description]);
}
@finally{
}
}
}