如何从 objective c 中的 XML 标签获取 JSON 数据

How to get JSON Data from XML tag in objective c

我正在尝试从服务器进行验证 代码是

-(IBAction)LoginbtnClick:(id)sender
{
    NSString* username = usertxt.text;
    NSString* pass = passtxt.text;


    if([usertxt.text isEqualToString:usertxtstr ]&& [passtxt.text isEqualToString:passtxtstr])
    {
        MenuScreen *MenuS =[[MenuScreen alloc] initWithNibName:@"MenuScreen" bundle:nil];
        [self.navigationController pushViewController:MenuS animated:YES];
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        [appDelegate.window setRootViewController:appDelegate.tab];
    }

    NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                             "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                             "<soap:Body>"
                             "<Login xmlns=\"http://tempuri.org/\">"
                             "<UserCode>%@</UserCode>"
                             "<Password>%@</Password>"
                             "</Login>"
                             "</soap:Body>"
                             "</soap:Envelope>",username,pass];

    NSData *postData = [soapMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];


    NSURL *url = [NSURL URLWithString:@"http://URL?op=Login"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPBody:postData];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
        webData = [[NSMutableData data] init];

    }
    else
    {

    }
    [usertxt resignFirstResponder];
    [passtxt resignFirstResponder];  
}
#pragma mark - NSURLConnection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    [webData setLength:0];


}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [webData appendData:data];


}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"loginStatus =%@",loginStatus);
    greeting.text = loginStatus;

}

#pragma mark - NSXMLParsing Delegate

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"UserCode"])
    {
        myDataClassObj=[[mydata alloc]init];
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

    myMutableStringSiteObj=[[NSMutableString alloc]initWithString:string];
    NSLog(@"Array String: %@",myMutableStringSiteObj);
    NSData *datasite = [myMutableStringSiteObj dataUsingEncoding:NSUTF8StringEncoding];
    responseSitedict = [NSJSONSerialization JSONObjectWithData:datasite options:0 error:nil];
    NSLog(@"JSON DATA = %@",responseSitedict);

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    NSLog(@"DataArray: %@",myDataNSMArray);
}

我得到的价值类似于

loginStatus =<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><LoginResponse xmlns="http://tempuri.org/"><LoginResult>[{"UserId":1,"UserCode":"abc","Password":[31,0,226,51,21,211,31,31,231,41,121,221,171,201,11,201,21,201,181,51],"SaltValue":[],"RoleId":0,"Salutation":"","FirstName":"abc","LastName":"xyz","EmailId":"abc@gmail.com","MobilNo":"9876543210","Status":true}]</LoginResult></LoginResponse></soap:Body></soap:Envelope>

我希望将用户代码和密码与我的用户名和密码进行比较,如果是真的,用户就可以登录。 不知道如何从 xml 标签中获取它。请帮助。

你可以这样传递你的字典

先转换成字典

 NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:loginStatus error:&parseError];
NSLog(@"JSON DICTIONARY = %@",xmlDictionary);
recordResult = [xmlDictionary[@"success"] integerValue];
NSLog(@"Success: %ld",(long)recordResult);

然后这样做

NSDictionary* Address=[xmlDictionary objectForKey:@"soap:Envelope"];
    NSLog(@"Address Dict = %@",Address);
    NSDictionary *new =[Address objectForKey:@"soap:Body"];
    NSLog(@"NEW DICT =%@",new);
    NSDictionary *LoginResponse=[new objectForKey:@"LoginResponse"];
    NSLog(@"Login Response DICT =%@",LoginResponse);
    NSDictionary *LoginResult=[LoginResponse objectForKey:@"LoginResult"];
    NSLog(@"Login Result = %@",LoginResult);
    NSLog(@"Login Result Dict =%@",LoginResult);
    testvalue.text =[LoginResult objectForKey:@"text"];
    NSString *teststr =[[NSString alloc] init];
    teststr=testvalue.text;
    NSLog(@"Test String Value =%@",teststr);
    NSString *string = [LoginResult valueForKey:@"text"];
    NSLog(@"Now String is =%@",string);
    NSData *data =[string dataUsingEncoding:NSUTF8StringEncoding];

    NSError* error;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:&error];
    NSDictionary *firstObj = [array objectAtIndex:0];

    NSString *UserCode = [firstObj valueForKey:@"UserCode"];
    NSLog(@"String value =%@",UserCode);
    NSString *userid =[firstObj valueForKey:@"UserId"];
    NSLog(@"User id is =%@",userid);
    NSString *Firstname =[firstObj valueForKey:@"FirstName"];
    NSLog(@"First Name is =%@",Firstname);
    NSString *Lastname=[firstObj valueForKey:@"LastName"];
    NSLog(@"Last Name is =%@",Lastname);
    NSString *Email =[firstObj valueForKey:@"EmailId"];
    NSLog(@"Email =%@",Email);
    NSString *Mobilenumber =[firstObj valueForKey:@"MobilNo"];
    NSLog(@"Mobile Number is =%@",Mobilenumber);

这是我所做的,可能您的代码不同。