如何从我的iPhone应用程序获取html文件的内容(JSON对象)?

时间:2022-01-25 17:36:57

I have an iPhone app which gets a response from a php file. This response generates an html file where the body contains a json object. How do I retrieve this JSON object ?

我有一个iPhone应用程序从php文件获取响应。此响应生成一个html文件,其中正文包含一个json对象。如何检索此JSON对象?

$query = "SELECT username FROM userData WHERE username = '$username'";  
    $result = mysql_query($query);  
    if (mysql_num_rows($result) > 0) { 
        sendResponse(200, json_encode('SUCCESS Notification'));
    } else { 
    $query = "INSERT INTO userData (username,password,email,signup_date) VALUES ('$username','$password','$email','$date')";
    $result=mysql_query($query,$connection) or die (mysql_error()." Kann Tabelle der Datenbank nicht lesen!");
    }  

    function sendResponse ($status = 200, $body ='', $content_type = 'application/json'){
            $status_header = 'HTTP/1.1 ' . $status . ' ' . 'OK';
            header($status_header);
            header('Content-type:' . $content_type);
            echo $body;
        }

This is what happens in my iphone app:

这是我的iPhone应用程序中发生的事情:

 NSURLResponse *theResponse =[[NSURLResponse alloc]init];
        NSError *error;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&error];      

         NSDictionary *jsonDictionaryResponse =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSLog(@"json response = %@", jsonDictionaryResponse);

3 个解决方案

#1


1  

As far as I can see, you don't add HTML-Tags around your JSON enocded string. So you could just parse the whole response body. I recommend you using jQuery (if your don't do already) or the JSON library.

据我所知,您不会在JSON enocded字符串周围添加HTML-Tags。所以你可以解析整个响应体。我建议你使用jQuery(如果你还没有)或JSON库。

But please note, that you should chage your MIME-Type to application/json when sending JSON data and that it doesn't make sense to json_encode a string.

但是请注意,在发送JSON数据时应该将MIME类型转换为application / json,并且对json_encode字符串没有意义。

sendResponse(200, json_encode(array('SUCCESS Notification')), 'application/json');

#2


0  

If its a properly formatted HTML (with start and end tags) then you can use any XML Parser like NSXMLParser or use HPPLE parser to parse HTML.

如果它是格式正确的HTML(带有开始和结束标记),那么您可以使用任何XML解析器(如NSXMLParser)或使用HPPLE解析器来解析HTML。

#3


0  

I'm not exactly sure to what purpose you are sending an HTML-file with JSON in it but that is not a good practice. HTML is a document and JSON is a data-structure wich you are now including as text in the body. Are you just using the JSON from the reponse and "throwing the rest away"? Cause then I agree with Pekka to change your output. I'm not sure how familiar you are with PHP but you can change the output of a page to anything you want. Just don't put any html in the php-file you are requesting and set the ouput-type to json like this:

我不确定你发送带有JSON的HTML文件的目的,但这不是一个好习惯。 HTML是一个文档,JSON是一个数据结构,您现在将其作为文本包含在正文中。你只是使用响应中的JSON并“抛弃其余部分”吗?因为我同意Pekka改变你的输出。我不确定你对PHP有多熟悉,但你可以将页面的输出更改为你想要的任何东西。只是不要在你要求的php文件中放入任何html,并将ouput-type设置为json,如下所示:

header('Content-type:text/json');

Otherwise you could also use regular expressions or the DOM-parser Pekka suggested.

否则你也可以使用正则表达式或Pekka建议的DOM解析器。

#1


1  

As far as I can see, you don't add HTML-Tags around your JSON enocded string. So you could just parse the whole response body. I recommend you using jQuery (if your don't do already) or the JSON library.

据我所知,您不会在JSON enocded字符串周围添加HTML-Tags。所以你可以解析整个响应体。我建议你使用jQuery(如果你还没有)或JSON库。

But please note, that you should chage your MIME-Type to application/json when sending JSON data and that it doesn't make sense to json_encode a string.

但是请注意,在发送JSON数据时应该将MIME类型转换为application / json,并且对json_encode字符串没有意义。

sendResponse(200, json_encode(array('SUCCESS Notification')), 'application/json');

#2


0  

If its a properly formatted HTML (with start and end tags) then you can use any XML Parser like NSXMLParser or use HPPLE parser to parse HTML.

如果它是格式正确的HTML(带有开始和结束标记),那么您可以使用任何XML解析器(如NSXMLParser)或使用HPPLE解析器来解析HTML。

#3


0  

I'm not exactly sure to what purpose you are sending an HTML-file with JSON in it but that is not a good practice. HTML is a document and JSON is a data-structure wich you are now including as text in the body. Are you just using the JSON from the reponse and "throwing the rest away"? Cause then I agree with Pekka to change your output. I'm not sure how familiar you are with PHP but you can change the output of a page to anything you want. Just don't put any html in the php-file you are requesting and set the ouput-type to json like this:

我不确定你发送带有JSON的HTML文件的目的,但这不是一个好习惯。 HTML是一个文档,JSON是一个数据结构,您现在将其作为文本包含在正文中。你只是使用响应中的JSON并“抛弃其余部分”吗?因为我同意Pekka改变你的输出。我不确定你对PHP有多熟悉,但你可以将页面的输出更改为你想要的任何东西。只是不要在你要求的php文件中放入任何html,并将ouput-type设置为json,如下所示:

header('Content-type:text/json');

Otherwise you could also use regular expressions or the DOM-parser Pekka suggested.

否则你也可以使用正则表达式或Pekka建议的DOM解析器。