0

I am facing some Pathetic Problem . Here I am Sharing my Code and Response ..

JSON Response :

JSON Log :

[
  {
    "0": "41",
    "intid": "41",
    "1": "\u8a00\u3046",
    "varfirstname": "\u8a00\u3046",
    "2": "test",
    "varlastname": "test",
    "3": "\u5730",
    "varusername": "\u5730",
    "4": "abc@gmail.com",
    "varemailid": "abc@gmail.com",
    "5": "qwert",
    "varpassword": "qwert",
    "6": "12345",
    "varmobileno": "12345",
    "7": "Enable",
    "mobileMessage": "Enable",
    "8": "",
    "varphoneno": "",
    "9": "Enable",
    "enumstatus": "Enable",
    "10": "2013-01-30",
    "date_insert": "2013-01-30",
    "11": "2013-01-30",
    "date_edit": "2013-01-30",
    "12": "210.211.252.1",
    "varipaddress": "210.211.252.1"
  }
]

From This Response I am Converting NSArray like this ..

// Create new SBJSON parser object 
SBJSON *parser = [[SBJSON alloc] init];  
NSArray *array = (NSArray *) [parser objectWithString:json_string error:nil];
NSLog(@"Array === %@",array);

And This Array Shows this Data :

Log :

Array ===
(
    {
    0 = 41;
    1 = "";
    10 = "2013-01-30";
    11 = "2013-01-30";
    12 = "210.211.252.1";
    2 = test;
    3 = "";
    4 = "abc@gmail.com";
    5 = qwert;
    6 = 12345;
    7 = Enable;
    8 = "";
    9 = Enable;
    "date_edit" = "2013-01-30";
    "date_insert" = "2013-01-30";
    enumstatus = Enable;
    intid = 41;
    mobileMessage = Enable;
    varemailid = "abc@gmail.com";
    varfirstname = "";
    varipaddress = "210.211.252.1";
    varlastname = test;
    varmobileno = 12345;
    varpassword = qwert;
    varphoneno = "";
    varusername = "";
})

Also I am Converting my VarfirstName Like this

NSDictionary *dataObject=[array objectAtIndex:0];
NSString *status = [dataObject objectForKey:@"varfirstname"];
NSLog(@"status -- %@",status);
NSString *myDecodedS = status;
NSString *myDecoded = [myDecodedS stringByReplacingPercentEscapesUsingEncoding:NSUTF16StringEncoding];
NSLog(@"HIIii %@",myDecoded);

Can Any one tell me how Could I Get data From That JSON Response ... I am not getting what I am doing wrong ?

4

3 回答 3

1

I copied your JSON into a text file, converted it back to data and used NSJSONSerialization class, and that worked fine:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSError *error;
    NSData *data = [[NSData alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"]];
    id jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"%@",jsonObj);
    NSLog(@"%@",jsonObj[0][@"varfirstname"]);
}

The log output was:

2013-02-01 22:47:40.361 JSONExperiment[4200:c07] (
        {
        0 = 41;
        1 = "\U8a00\U3046";
        10 = "2013-01-30";
        11 = "2013-01-30";
        12 = "210.211.252.1";
        2 = test;
        3 = "\U5730";
        4 = "abc@gmail.com";
        5 = qwert;
        6 = 12345;
        7 = Enable;
        8 = "";
        9 = Enable;
        "date_edit" = "2013-01-30";
        "date_insert" = "2013-01-30";
        enumstatus = Enable;
        intid = 41;
        mobileMessage = Enable;
        varemailid = "abc@gmail.com";
        varfirstname = "\U8a00\U3046";
        varipaddress = "210.211.252.1";
        varlastname = test;
        varmobileno = 12345;
        varpassword = qwert;
        varphoneno = "";
        varusername = "\U5730";
    }
)
2013-02-01 22:47:40.363 JSONExperiment[4200:c07] 言う
于 2013-02-02T06:49:59.767 回答
0

varfirstname key store the result as a Unicode string

Try this , I think your problem solve

For String

NSString *status = [NSString stringWithUTF8String:[[dataObject objectForKey:@"varfirstname"] UTF8String]]);

NSLog(@"status -- %@",status); 

For Json Try this -------------------------------------

   SBJsonParser *parser = [[SBJsonParser alloc] init];

NSURLRequest *UrlRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://eappointmentbook.net/iphone/getAccountInfo.php?userid=41&usertype=Normal%20User"]];

NSData *response = [NSURLConnection sendSynchronousRequest:UrlRequest  returningResponse:nil error:nil];

NSString *json_string = [[NSString alloc]  initWithData:response encoding:NSUTF8StringEncoding];

NSArray *status = [parser objectWithString:json_string error:nil];


NSLog(@"status Array  %@ " , status);

NSLog(@"status varfirstname  %@ " , [[status objectAtIndex:0]objectForKey:@"varfirstname"]);
于 2013-02-02T05:46:42.437 回答
0

I downloaded latest version of SBJSON 3.1.1 and saved your json string in a file and it worked for me

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSString *str =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str);
NSArray *array = (NSArray *) [parser objectWithString:str error:nil];
NSLog(@"Array === %@",array);

Here are my logs

2013-02-02 12:20:31.931 MyTab[1343:c07] [{"0":"41","intid":"41","1":"\u8a00\u3046","varfirstname":"\u8a00\u3046","2":"test","varlastname":"test","3":"\u5730","varusername":"\u5730","4":"abc@gmail.com","varemailid":"abc@gmail.com","5":"qwert","varpassword":"qwert","6":"12345","varmobileno":"12345","7":"Enable","mobileMessage":"Enable","8":"","varphoneno":"","9":"Enable","enumstatus":"Enable","10":"2013-01-30","date_insert":"2013-01-30","11":"2013-01-30","date_edit":"2013-01-30","12":"210.211.252.1","varipaddress":"210.211.252.1"}]
2013-02-02 12:20:31.933 MyTab[1343:c07] Array === (
        {
        0 = 41;
        1 = "\U8a00\U3046";
        10 = "2013-01-30";
        11 = "2013-01-30";
        12 = "210.211.252.1";
        2 = test;
        3 = "\U5730";
        4 = "abc@gmail.com";
        5 = qwert;
        6 = 12345;
        7 = Enable;
        8 = "";
        9 = Enable;
        "date_edit" = "2013-01-30";
        "date_insert" = "2013-01-30";
        enumstatus = Enable;
        intid = 41;
        mobileMessage = Enable;
        varemailid = "abc@gmail.com";
        varfirstname = "\U8a00\U3046";
        varipaddress = "210.211.252.1";
        varlastname = test;
        varmobileno = 12345;
        varpassword = qwert;
        varphoneno = "";
        varusername = "\U5730";
    }
)
于 2013-02-02T06:53:53.783 回答