1

我正在创建一个简单的登录页面。在我的项目中,我想解析 json 字符串。但它给了我以下错误。

-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=4 
\"Valid fragment, but not JSON\" 
UserInfo=0xa6e70a0 {NSLocalizedDescription=Valid fragment, but not JSON}"

在我的代码中,如果我放置另一个 json 字符串而不是它正在工作。而且我原来的 json 字符串也给了我浏览器中的数据。那么该怎么办?

我的代码是:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://dev.bevbucks.com/gbs/api.json/token?user=rverma@prismetric.com&pwd=verma!pris"]];
NSURLResponse *response = nil;
NSError *error = nil;
//getting the data
NSData *newData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//json parse

NSString *responseString = [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
NSDictionary *jsonObject = [responseString JSONValue];
NSLog(@"type :  %@", jsonObject  ); 
4

1 回答 1

2

返回的字符串:

"60ee094456b6fc03f386af50c443b471"

不是有效的 JSON,至少应该是:

[ "60ee094456b6fc03f386af50c443b471" ]

所以服务器中存在错误,而不是您的代码。如果您无法修复此错误,那么您将不得不解决它。

来自JSON.org

JSON 建立在两种结构之上:

  • 名称/值对的集合。在各种语言中,这被实现为对象、记录、结构、字典、哈希表、键控列表或关联数组。

  • 值的有序列表。在大多数语言中,这被实现为数组、向量、列表或序列。

于 2013-03-07T13:25:57.763 回答