json_encode()处理单个对象但不处理数组

时间:2016-07-17 16:49:55

标签: php arrays json

我正在使用一系列新闻。我的新闻对象就像这样

class News
{
    public $id;
    public $title;
    public $content;
    public function __construct($id,$title,$content)
    {
        $this->id = $id;
        $this->title = $title;
        $this->content = $content;
    }
}

我正在尝试编码一个我从数据库获取的新闻数组,但它返回一个空响应而var_dumps显示数据存在

$newsarray = array();
while($newsdata = $result->fetch_assoc())
{
    $news = new News($newsdata["id"],$newsdata["title"],$newsdata["content"]);
    array_push($newsarray,$news);
}
header('Content-Type: application/json');

echo json_encode($newsarray);

但是,当我对一个对象进行处理时,它会提供一个非常好的json数据

if($newsdata = $result->fetch_assoc())
{
    require_once("objects/news.php");
    $news = new News($newsdata["id"],$newsdata["title"],$newsdata["content"]);
    header('Content-Type: application/json');
    echo json_encode($news);
}

我知道我在这里犯了一些非常愚蠢的错误。我用Google搜索但没有帮助。任何帮助都会很好

编辑 的print_r($ queryarray)

Array
(
    [0] => News Object
        (
            [id] => 1
            [title] =>  Gloucestershire collapse leaves Essex scenting victory
            [content] =>  A day of which some thought would confirm Essex as favourites to take Division Two's single promotion place eventually did so but not without suggesting the type of resistance the leaders must overcome if they are to prevail.

After a long day which warmed and brightened and even, at its best, confirmed the arrival of summer, Ryan ten Doeschate's batsmen need another 197 runs with nine wickets in hand to clinch a victory that would give them a 23-point lead at the top of the table. Essex are in a dominant position but the loss of Jaik Mickleburgh's wicket three overs before the close will have reminded the visitors that there is still serious work ahead of them.

But the rather pleasant state of affairs for supporters travelling from Colchester and Clacton followed two very fruitful sessions for home loyalists in which Gloucestershire's batsmen had made light of a 78-run first-innings deficit and had themselves established a decent lead of 153 runs with seven wickets in hand at tea.

Gareth Roderick's century and Chris Dent's increasingly assured 72 had fortified the hopes of those frequenting the hopes of those in the Churchdown and Charlton Kings marquees that Essex might be set a stiff target on the final day of this match.

However, once the openers were out, ten Doeschate's bowlers greedily exploited the weakness of Gloucestershire's later batsmen and the home side lost their last eight wickets for 62 runs with Matt Quinn removing four of the top seven batsmen to end with a match analysis of 11 for 63, comfortably the best of his career.

Graham Napier also showed what a loss he will be to Essex when he retires at the end of the season by making short work of the Gloucestershire tailenders. The combination of Quinn's hostility and Napier's movement finally broke the home side.

Foster took three catches off Napier and Quinn won a lively red-blooded exchange with Craig Miles, who whacked him for six over midwicket before giving a catch to ten Doeschate at leg slip next ball. Michael Klinger was left undefeated on 53 and three of Gloucestershire's batsmen scored nearly 80% of their team's runs  
        )

    [1] => News Object
        (
            [id] => 2
            [title] =>   CSA cuts Ackerman as host for awards ceremony
            [content] =>  CSA has removed former South Africa batsman HD Ackerman as the host of its upcoming annual awards ceremony in a bid to to avoid "further potential negative comments" around his announcement that he was emigrating to Australia in September. Ackerman is the fourth South African involved in the country's cricket structures to move to Australia or New Zealand in the last two months.

"In view of certain negative comments on social media following HD's Facebook post that he was leaving South Africa for Australia, we have decided to avoid any further potential negative comments around HD hosting the live CSA Awards," Altaaf Kazi, CSA spokesperson, told Sport24. "We decided to replace him and have no further comments to make in this regard."

CSA has not revealed who it has appointed as Ackerman's replacement.

Ackerman had announced his decision to emigrate through a post on Facebook: "It is with a heavy heart that I have to let all my friends know that Kerryn Ackerman, James, Tannah and I will be moving to Perth, Australia in September. It has caused many sleepless nights and even more tears but it is a decision that we have reached as a family and feel it is the best one we can make for our little ones at this time.    
        )

    [2] => News Object
        (
            [id] => 5
            [title] => गएरातिको वर्षापछि तमोर नदी थुनिएरको ��
            [content] => श्रावण १, २०७३- ताप्लेजुङको तमोर नदीमा गएराति आएको बाढीले दोभान बजारमा निर्माणाधीन मोटरेबल पुल बगाएको छ । राति १२ देखि १ बजेको बिचमा आएको बाढीले पूल बगाएको स्थानीय व्यवसायी टंक खतिवडाले जानकारी दिए ।

ताप्लेजुङको क्षेत्र नम्बर १ र २ जोड्ने उक्त पुलको वारिपारी जोड्ने काम सकिएको थियो । १० करोडको लागतमा निर्माणाधीन पुलको ९० प्रतिशत काम सकिएको उत्तर दक्षिण लोकमार्ग आयोजनाका प्रमूख कूबेर नेपालीले बताए ।

नदीमा पानीको सतह अहिले बिहान केही घटेको र मानवीय बस्तिलाई असर नपारेको प्रमूख जिल्ला अधिकारी चक्रपाणी पाण्डेले बताए । सरकारी टोलि अध्ययनकालागि अहिले बिहान पुल बगाएको क्षेत्रमा गएको छ ।

उता असार २५ गते सावादिनको नाक्ला भीर फुटेर  मित्लुङ बजार छेउमा बनेको ताल अझै बढेको स्थानीयले बताएका छन् । गएराति पनि पहरो थप झरेर तालको सतह अझ बढाएको मित्लुङका पवन श्रेष्ठले जानकारी दिए ।
        )

    [3] => News Object
        (
            [id] => 6
            [title] =>  पारसको प्रभाव, इङ्ल्यान्ड भ्रमणमा ने�
            [content] =>   यसअघि सीसीसीको सुरुआत पनि राम्रो थियो । सागर पटेलले र किशन दाससँग मिलेर पहिलो विकेटमा १० ओभर भित्र ३५ रन जोडे । किशन १५ रन बनाई करण केसीको बलमा आउट भए । अहमद इलेचलाई पारसले ५ रनमा बोल्ड गरे । जेम्स साउथगेट १५ रन बनाई सन्दीप लामिछानेको शिकार भए ।

सागर र स्टिभ सेलउडले चौथो विकेटमा ५० रन जोडे । सन्दीपकै बलमा स्टम्प हुनु अगाडी सागरले ८२ बल खेली ६ चौका र १ छक्कासहित ६५ रन बनाए ।

डर्बीसायरका पूर्व खेलाडी सेलउडले ६७ रन बनाए । ७३ बलको इनिङ्समा उनले ५ चौका र १ छक्का प्रहार गरे । सोमपाल कामीले उनलाई आउट गरे । सोमपालले अन्तिम ओभरमा निक स्टेभन्सलाई ९ रनमा फ्लो थ्रुमा रनआउट गरे भने एन्ड्रु वेस्टफललाई शुन्यमा बोल्ड गरे । सोमपाल र सन्दीपले १० ओभर पुरा बलिङमा क्रमश ३८ र ३६ रन खर्चे । पारस र करणले ६ ओभरमा क्रमश २५ र ३५ रन दिए । शक्ति गौचन र सागरले ७ ओभरमा क्रमश २४ र ३४ तथा ज्ञानेन्द्रले ४ ओभरमा २७ रन खर्चे ।
        )

)

2 个答案:

答案 0 :(得分:0)

正如axiac所指出的那样。错误发生在我的数据中。一旦我从我的数据库中删除了我的区域数据。它开始运作良好。

答案 1 :(得分:0)

将数组转换为json_encode时,请确保使用字符集。

如果您使用的是mysqli,请在mysqli语句之前添加以下代码(选择,删除...)

mysqli_set_charset($conn,'utf8');

这会在插入数据库时​​将特殊字符转换为utf8格式。