2016-07-17 10 views
2

Ich arbeite mit einem Array von News. Meine Nachrichten Objekt ist wie dieserjson_encode() arbeitet an einem einzelnen Objekt, aber nicht am Array

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

Und ich versuche, eine Reihe von Nachrichten zu codieren, dass ich aus der Datenbank erhalten, aber seine Rückkehr eine leere Antwort Und var_dumps zeigt die Daten anwesend sein

$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); 
Aber

, wenn ich es auf ein einzelnes Objekt zu tun gibt es eine ziemlich gute json Daten

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); 
} 

ich weiß, ich mache einige sehr dumme Fehler hier. Ich habe es gegoogelt aber keine Hilfe. Jede Hilfe wäre schön

bearbeiten 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] => यसअघि सीसीसीको सुरुआत पनि राम्रो थियो । सागर पटेलले र किशन दाससँग मिलेर पहिलो विकेटमा १० ओभर भित्र ३५ रन जोडे । किशन १५ रन बनाई करण केसीको बलमा आउट भए । अहमद इलेचलाई पारसले ५ रनमा बोल्ड गरे । जेम्स साउथगेट १५ रन बनाई सन्दीप लामिछानेको शिकार भए । 

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

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

) 
+0

Sie müssen möglicherweise mit dem dritten Argument zu 'json_encode'' $ depth' herumspielen ... besonders, wenn Sie eine verschachtelte Datenstruktur haben .... lesen Sie die Dokumentation: http://php.net/manual /de/function.json-encode.php – Poiz

+0

@FirstOne ok. Ich werde versuchen –

+0

Versuchen Sie, [JsonSerializable] (http://php.net/manual/en/class.jsonserializable.php) zu implementieren. – FirstOne

Antwort

0

Wie axiac hingewiesen. Der Fehler war in meinen Daten. Sobald ich meine regionalen Daten aus meiner Datenbank entfernt habe. Es begann gut zu funktionieren.

0

Beim Konvertieren von Array in json_encode stellen Sie sicher, dass Sie den Zeichensatz verwenden.

Wenn Sie mysqli verwenden, dann unter Code vor Mysqli Anweisung hinzufügen (Select, löschen ...)

mysqli_set_charset($conn,'utf8'); 

Sonderzeichen in UTF-8-Format Dieses konvertieren, während in die Datenbank einfügen.

Verwandte Themen