php - Multiple JSON objects from text file -


hello i'm trying use mutiple json objects shown in example below.

[   {     "dateandtime" : "1025",     "logginglevel" : "error",     "description" : "test"   } ] [   {     "dateandtime" : "1025",     "logginglevel" : "error",     "description" : "test"   } ] 

this how it's created ios side because create 1 of objects @ time because it's report logging system , need message passed when it's needed. json objects created @ separate times , appended file.

i know valid json string below.

[   {     "dateandtime" : "1025",     "logginglevel" : "error",     "description" : "test"   },    {     "dateandtime" : "1025",     "logginglevel" : "error",     "description" : "test"   } ] 

however that's not need. there way of using 2 separate json objects?

ios

    nsstring *datastring = [nsstring stringwithformat:@"{ \"dateandtime\":\"%@\", \"description\":\"%@\", \"logginglevel\":\"%@\" }", @"1025", logstring, [self getlogtypename:(logs)level]];     nsmutablearray *currentdata = [nsjsonserialization jsonobjectwithdata:[datastring datausingencoding:nsutf8stringencoding] options:nsjsonreadingmutablecontainers error:nil];     nsmutablearray *arrayofdata = [[nsmutablearray alloc] init];     [arrayofdata addobject:currentdata];     nsdata *jsonobject = [nsjsonserialization datawithjsonobject:arrayofdata options:0 error:nil];     nsstring *jsonstring = [[nsstring alloc] initwithdata:jsonobject encoding:nsutf8stringencoding];      post = [nsstring stringwithformat:@"message=%@", jsonstring]; 

php

$file = 'testingfile.txt'; // open file existing content $current = file_get_contents($file); if (isset($_post['message'])) { // append new person file $current .= $_post['message'] . php_eol;  // write contents file file_put_contents($file, $current);  } else {     $contents = file_get_contents($file);     echo $contents; } 

javascript

function getloggingdata() {   $.get("../isosec/logging/testphp.php", function(data){       $.each($.parsejson(data), function(idx, obj) {          console.log(obj.dateandtime);          console.log(obj.logginglevel);          console.log(obj.description);          addlog(obj.dateandtime, obj.logginglevel, obj.description);       });    }); } 

could show me how merge objects if there json object in file or there other work around?

thanks.

as mentioned in comment, better off generating file valid json rather trying parse own json syntax.

you can parsing existing json , appending objects needed.

conceptual (not tested) example:

nsmutablearray *currentdata = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers errors:&errors]; [currentdata addobject:newobject]; nsdata *updatedjsondata = [nsjsonserialization datawithjsonobject:currentdata options:0 errors:&errors]; 

update

okay, far see question, have 2 environments. have client , server. didn't catch individual log messages handled php script.

here's in app:

nserror *error;  // simplify generation of dictionary nsdictionary *logline = @{     @"dateandtime": @"1024"     @"description": logstring,     @"logginglevel": [self getlogtypename:(logs)level] };  // create json string dictionary nsdata *jsondata = [nsjsonserialization datawithjsonobject:logline options:0 error:&error]; nsstring *jsonstr = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];  // post jsonstr http body php script 

and in script:

<?php  $logfilename = 'testingfile.txt';  // read current log file contents $currentlogjson = file_get_contents($logfilename);  // check log contents if (empty($currentlogjson)) {     $currentlog = []; } else {     $currentlog = json_decode($currentlogjson);     // ensure contents of log file array     if (is_array($currentlog)) {         $currentlog = [];     } }  // new log line http body $newloglinejson = file_get_contents('php://input'); // decode log line passed json string $newlogline = json_decode($newlogline); if (json_last_error() == json_error_none) {     // append log line current log     $currentlog[] = $newlogline;     // write updated log contents log file     file_put_contents($logfilename, json_encode($currentlog)); } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -