【问题标题】:Getting JSON html data获取 JSON html 数据
【发布时间】:2021-02-07 07:02:30
【问题描述】:

我正在从 word press 数据库中获取 JSON 数据,以将它们添加到使用 android studio 的移动应用程序中。然而,我面临着许多问题。 首先,我需要正确查看帖子内容,但是 html 标签呢? 搜索后我添加了 JSON_HEX_TAG | JSON_PRETTY_PRINT | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE 到我的输出,但这并没有解决我的问题。 =

PHP 代码

<?php 
    header('content-type: application/json; charset=utf-8');
    /*
    * Created by Belal Khan
    * website: www.simplifiedcoding.net 
    * Retrieve Data From MySQL Database in Android
    */
    
    //database constants
    define('DB_HOST', 'localhost');
    define('DB_USER', '');
    define('DB_PASS', '');
    define('DB_NAME', '');
    
    //connecting to database and getting the connection object
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    
    //Checking if any error occured while connecting
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        die();
    }
    
    
    function getAttachment($post_title) {
        
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $stmt = $conn->prepare("SELECT guid,post_title  FROM wp_posts  WHERE `post_type`='attachment'  AND `post_title` = ?");
    $conn -> set_charset("utf8");
    $stmt->bind_param("s",$post_title);
    $stmt->execute();
    $stmt->store_result();    
    $stmt->bind_result($guid, $post_title); 
    $products_attach = array(); 
    if($stmt->num_rows == 1) {
    while($stmt->fetch()){
        $temp = array();
        $temp['guid'] = $guid; 
    

        array_push($products_attach, $temp);
    }
 return     $temp['guid'];
}
}

 

    
        
        
    
    
    //creating a query
    $stmt = $conn->prepare("SELECT ID, post_title, post_content  FROM wp_posts  WHERE `post_type`='lsvr_document'AND `post_status`= 'publish';");
    $conn -> set_charset("utf8");

    //executing the query 
    $stmt->execute();
    
    //binding results to the query 
    $stmt->bind_result($id, $title, $content);
    
    $products = array(); 
    
    //traversing through all the result 
    while($stmt->fetch()){
        $temp = array();
        $temp['id'] = $id; 
        $temp['title'] = $title; 
        $temp['link'] = getAttachment($temp['title']);
        $temp['content'] = $content; 
 
        array_push($products, $temp);
    }
    
    //displaying the result in json format 
//  echo json_encode($products, JSON_UNESCAPED_UNICODE  | JSON_HEX_TAG );


 $json = json_encode($products, JSON_HEX_TAG | JSON_PRETTY_PRINT  | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);

echo $json;
//print_r($products);

    
?>

部分输出

       "id": 867,
    "title": "إفادة إنجاز بناء مخالف",
    "link": "http:\/\/syncsecser.com\/municipal\/wp-content\/uploads\/2020\/09\/إفادة-إنجاز-بناء-مخالف.pdf",
    "content": "\u003C!-- wp:heading --\u003E\n\u003Ch2\u003Eالمستندات المرفقة و أماكن إنجازها\u003C\/h2\u003E\n\u003C!-- \/wp:heading --\u003E\n\n\u003C!-- wp:paragraph --\u003E\n\u003Cp\u003E1. طلب موقع من صاحب العلاقة أو من ينوب عنه قانوناً.\u003Cbr\u003E2. إفادة عقارية\u0026nbsp;شاملة.\u003Cbr\u003E3. أي مستند يثبت تاريخ إنجاز المخالفة.\u003C\/p\u003E\n\u003C!-- \/wp:paragraph --\u003E\n\n\u003C!-- wp:table {\u0022className\u0022:\u0022is-style-regular\u0022} --\u003E\n\u003Cfigure id=\u0022table1\u0022 class=\u0022wp-block-table is-style-regular\u0022\u003E\u003Ctable\u003E\u003Ctbody\u003E\u003Ctr\u003E\u003Ctd\u003E\u003Cbr\u003E\u003Cbr\u003E-\u003Cstrong\u003E\u003Cu\u003Eمدّة\u0026nbsp;الإنجاز :\u003C\/u\u003E\u003C\/strong\u003E\u003Cbr\u003E\u0026nbsp;ثلاثة أيام سنداً\u0026nbsp;لتعميم وزير الداخلية والشؤون البلدية والقروية رقم 8\/99.\u003Cbr\u003E\u003Cbr\u003E-\u003Cstrong\u003E\u0026nbsp;الرسوم\u0026nbsp;المتوجبة\u003Cbr\u003E\u003Cbr\u003E\u0026nbsp;\u003C\/strong\u003E\u003Cbr\u003E1.\u0026nbsp;\u0026nbsp;رسم الإفادة الفنية:\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp; 2000 ل.ل.\u003Cbr\u003E\u0026nbsp;\u003Cbr\u003E2.\u0026nbsp;\u0026nbsp;رسم طابع مالي:\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;1000 ل.ل.\u003Cbr\u003E\u003C\/td\u003E\u003C\/tr\u003E\u003C\/tbody\u003E\u003C\/table\u003E\u003C\/figure\u003E\n\u003C!-- \/wp:table --\u003E"
},

【问题讨论】:

  • 请用一句话描述您的主要问题。定义“正确”。 HTML标签有什么问题?
  • 是的 html 标签,但我想将内容分成列表和 h1

标签: java php android json


【解决方案1】:

一个简单的解决方案是您可以将您的内容编码为 base64,然后在您的移动应用程序或您想要解析内容的任何其他页面中,您只需将此 base64 解码即可。

【讨论】:

  • 如何解析内容?
  • 在 PHP 中,您可以在 base64 编码的内容上使用base64_decode($str)
  • 是的,这是在 php api 中,但是如何从移动应用程序的另一端解析它
  • 试试this@cccwsb
猜你喜欢
  • 2019-09-16
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
  • 2023-03-30
  • 1970-01-01
相关资源
最近更新 更多