首页 > 解决方案 > 如何获取json格式的html?

问题描述

我有一个 PHP API,可以将数据返回到我的 android 应用程序,数据库中的一些数据是 Html 格式的。有时 API 返回所有内容,但返回一些Html 数据时,API 没有返回任何内容,这就是 API。

//Importing Database Script 
require_once('dbConnect.php');
//Creating sql query
$sql="SELECT * FROM `blogs`";
//getting result
$r = mysqli_query($con,$sql);

//creating a blank array 
$result = array();

//looping through all the records fetched
while($row = mysqli_fetch_array($r)){

    //Pushing name and id in the blank array created 
    array_push($result,array(
        "id"=>$row['id'],
        "title"=>$row['title'],
        "details"=>$row['details'],
        "image"=>$row['photo'],
        "source"=>$row['source'],
        "views"=>$row['views'],
        "date"=>$row['created_at']

    ));
}

//Displaying the array in json format 
echo json_encode(array('result'=>$result));

mysqli_close($con);

注意标题和描述是 Html 标题返回但细节不是

这是无效数据的示例

<div align="justify">The recording starts with the patter of a summer squall. Later, a 
drifting tone like that of a not-quite-tuned-in radio station 
                                        rises and for a while drowns out
 the patter. These are the sounds encountered by NASA’s Cassini 
spacecraft as it dove 
                                        the gap between Saturn and its 
innermost ring on April 26, the first of 22 such encounters before it 
will plunge into 
                                        atmosphere in September. What 
Cassini did not detect were many of the collisions of dust particles 
hitting the spacecraft
                                        it passed through the plane of 
the ringsen the charged particles oscillate in unison.<br><br></div><h3 align="justify">How its Works ?</h3>
                                    <p align="justify">
                                        MIAMI — For decades, South 
Florida schoolchildren and adults fascinated by far-off galaxies, 
earthly ecosystems, the proper
                                        ties of light and sound and 
other wonders of science had only a quaint, antiquated museum here in 
which to explore their 
                                        interests. Now, with the 
long-delayed opening of a vast new science museum downtown set for 
Monday, visitors will be able 
                                        to stand underneath a suspended,
 500,000-gallon aquarium tank and gaze at hammerhead and tiger sharks, 
mahi mahi, devil
                                        rays and other creatures through
 a 60,000-pound oculus. <br></p><p align="justify">Lens that will give the impression of seeing the fish from the bottom of
 a huge cocktail glass. And that’s just one of many
                                        attractions and exhibits. 
Officials at the $305 million Phillip and Patricia Frost Museum of 
Science promise that it will be a 
                                        vivid expression of modern 
scientific inquiry and exposition. Its opening follows a series of 
setbacks and lawsuits and a 
                                        scramble to finish the 
250,000-square-foot structure. At one point, the project ran 
precariously short of money. The museum
                                        high-profile opening is 
especially significant in a state s <br></p><p align="justify"><br></p><h3 align="justify">Top 5 reason to choose us</h3>
                                    <p align="justify">
                                        Mauna Loa, the biggest volcano 
on Earth — and one of the most active — covers half the Island of 
Hawaii. Just 35 miles to the 
                                        northeast, Mauna Kea, known to 
native Hawaiians as Mauna a Wakea, rises nearly 14,000 feet above sea 
level. To them it repre
                                        sents a spiritual connection 
between our planet and the heavens above. These volcanoes, which have 
beguiled millions of 
                                        tourists visiting the Hawaiian 
islands, have also plagued scientists with a long-running mystery: If 
they are so close together, 
                                        how did they develop in two 
parallel tracks along the Hawaiian-Emperor chain formed over the same 
hot spot in the Pacific 
                                        Ocean — and why are their 
chemical compositions so different? "We knew this was related to 
something much deeper,
                                        but we couldn’t see what,” said 
Tim Jones.
                                    </p>

标签: phpandroidapi

解决方案


确保在将任何 HTML 数据放入 JSON 数组之前对其进行正确编码,否则可能会破坏它。利用htmlentities($html)

例如。"source"=>htmlentities($row['source'])


推荐阅读