你好我们面临与将字符串转换为json相关的问题。 这是我的字符串:
[
{
"SCHEME_NAME": "FUG RSA SCHEME",
"Investment_Value": -46719.00201558,
"Bid_Price": 2.2566,
"Total_Contributions": 0,
"Growth": -46719.00201558,
"INVESTOR_ID": 5613,
"PFA_SCHEMEID": 1
},
{
"MONTH_NAME": "Balance as at 07-07-2016",
"EMPLOYEE_CONTRIBUTION": 3433764.77,
"EMPLOYER_CONTRIBUTION": 4381387.29,
"TOTAL_VALUE": 7815152.06,
"TOTAL_UNITS": 2782788.3885,
"TOTAL_FEE": 0,
"TOTAL_CONTRIBUTION": 7815152.06,
"Voluntary": "0.00"
},
{
"MONTH_NAME": "July 2016",
"EMPLOYEE_CONTRIBUTION": 0,
"EMPLOYER_CONTRIBUTION": 0,
"TOTAL_VALUE": 0,
"TOTAL_UNITS": -20703.2713,
"TOTAL_FEE": 0,
"TOTAL_CONTRIBUTION": 0,
"Voluntary": "0.00"
}
]
如何将其转换为JSON? 请帮帮我。
答案 0 :(得分:0)
试试这个:
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
答案 1 :(得分:0)
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:&error];
答案 2 :(得分:0)
JSON只不过是用括号{}括号括起来的一对键/值对,但是你错过了一对。您还需要一个封闭的根{}花括号并指定数组的键,目前我已根据您的要求指定了响应,然后JSON才有效。
{
"response": [
...
]
}
然后你可以用@SaintThread提到的内容进行转换。
答案 3 :(得分:0)
它已经是JSON格式。我试图将字符串转换为json。我得到了它。但结果与它的问题相同。
将字符串转换为json时,它应该位于\" Key \":\" Value \"或\"键\":值
NSString *str=@"[{\"SCHEME_NAME\":\"FUG RSA SCHEME\",\"Investment_Value\":-46719.00201558,\"Bid_Price\":2.2566,\"Bid_Price\":\"2.2566\",\"Total_Contributions\":0,\"Growth\":-46719.00201558,\"INVESTOR_ID\":5613,\"PFA_SCHEMEID\":1},{ \"MONTH_NAME\": \"Balance as at 07-07-2016\",\"EMPLOYEE_CONTRIBUTION\": 3433764.77,\"EMPLOYER_CONTRIBUTION\": 4381387.29,\"TOTAL_VALUE\": 7815152.06,\"TOTAL_UNITS\": 2782788.3885,\"TOTAL_FEE\": 0,\"TOTAL_CONTRIBUTION\": 7815152.06,\"Voluntary\": \"0.00\"},{\"MONTH_NAME\": \"July 2016\",\"EMPLOYEE_CONTRIBUTION\": 0,\"EMPLOYER_CONTRIBUTION\":0,\"TOTAL_VALUE\":0,\"TOTAL_UNITS\": -20703.2713,\"TOTAL_FEE\": 0,\"TOTAL_CONTRIBUTION\": 0,\"Voluntary\": \"0.00\"}]";
NSData *dataStr = [str dataUsingEncoding:NSUTF8StringEncoding];
id jsonData = [NSJSONSerialization JSONObjectWithData:dataStr options:0 error:nil];
NSLog(@"The converted string to json is %@",jsonData);
现在打印结果是
The converted string to json is (
{
"Bid_Price" = "2.2566";
Growth = "-46719.00201558";
"INVESTOR_ID" = 5613;
"Investment_Value" = "-46719.00201558";
"PFA_SCHEMEID" = 1;
"SCHEME_NAME" = "FUG RSA SCHEME";
"Total_Contributions" = 0;
},
{
"EMPLOYEE_CONTRIBUTION" = "3433764.77";
"EMPLOYER_CONTRIBUTION" = "4381387.29";
"MONTH_NAME" = "Balance as at 07-07-2016";
"TOTAL_CONTRIBUTION" = "7815152.06";
"TOTAL_FEE" = 0;
"TOTAL_UNITS" = "2782788.3885";
"TOTAL_VALUE" = "7815152.06";
Voluntary = "0.00";
},
{
"EMPLOYEE_CONTRIBUTION" = 0;
"EMPLOYER_CONTRIBUTION" = 0;
"MONTH_NAME" = "July 2016";
"TOTAL_CONTRIBUTION" = 0;
"TOTAL_FEE" = 0;
"TOTAL_UNITS" = "-20703.2713";
"TOTAL_VALUE" = 0;
Voluntary = "0.00";
}
)
然后当我将数据导入数组
NSArray *arrJson = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
NSLog(@"The arrjson is %@",arrJson);
打印结果是
The arrjson is (
{
"Bid_Price" = "2.2566";
Growth = "-46719.00201558";
"INVESTOR_ID" = 5613;
"Investment_Value" = "-46719.00201558";
"PFA_SCHEMEID" = 1;
"SCHEME_NAME" = "FUG RSA SCHEME";
"Total_Contributions" = 0;
},
{
"EMPLOYEE_CONTRIBUTION" = "3433764.77";
"EMPLOYER_CONTRIBUTION" = "4381387.29";
"MONTH_NAME" = "Balance as at 07-07-2016";
"TOTAL_CONTRIBUTION" = "7815152.06";
"TOTAL_FEE" = 0;
"TOTAL_UNITS" = "2782788.3885";
"TOTAL_VALUE" = "7815152.06";
Voluntary = "0.00";
},
{
"EMPLOYEE_CONTRIBUTION" = 0;
"EMPLOYER_CONTRIBUTION" = 0;
"MONTH_NAME" = "July 2016";
"TOTAL_CONTRIBUTION" = 0;
"TOTAL_FEE" = 0;
"TOTAL_UNITS" = "-20703.2713";
"TOTAL_VALUE" = 0;
Voluntary = "0.00";
}
)
答案 4 :(得分:-2)
试试这个在线json查看器:
您可以在线格式化和查看
这是一种格式
[
{
"SCHEME_NAME": "FUG RSA SCHEME",
"Investment_Value": -46719.00201558,
"Bid_Price": 2.2566,
"Total_Contributions": 0.00,
"Growth": -46719.00201558,
"INVESTOR_ID": 5613,
"PFA_SCHEMEID": 1
}
,
{
"MONTH_NAME": "Balance as at 07-07-2016",
"EMPLOYEE_CONTRIBUTION": 3433764.77,
"EMPLOYER_CONTRIBUTION": 4381387.29,
"TOTAL_VALUE": 7815152.06,
"TOTAL_UNITS": 2782788.3885,
"TOTAL_FEE": 0.00,
"TOTAL_CONTRIBUTION": 7815152.06,
"Voluntary": "0.00"
},
{
"MONTH_NAME": "July 2016",
"EMPLOYEE_CONTRIBUTION": 0.00,
"EMPLOYER_CONTRIBUTION": 0.00,
"TOTAL_VALUE": 0.00,
"TOTAL_UNITS": -20703.2713,
"TOTAL_FEE": 0.00,
"TOTAL_CONTRIBUTION": 0.00,
"Voluntary": "0.00"
}
]