javascript - Loading Json Objects from text file - Returning Uncaught SyntaxError: Unexpected token " [ " -


i'm having problem getting json objects text file , being able use them in javascript function. works if there 1 json object. when start having multiple json objects gives me error: uncaught syntaxerror: unexpected token [

here code below:

ios

    nsstring *post = @"";     nsdictionary *firstjsondictionary = [nsdictionary dictionarywithobjectsandkeys:@"1025", @"dateandtime", logstring, @"description", [self getlogtypename:(logs)level], @"logginglevel", nil];     nsmutablearray *arraytest = [[nsmutablearray alloc] init];      [arraytest addobject:firstjsondictionary];     nsdata *jsondata2 = [nsjsonserialization datawithjsonobject:arraytest options:nsjsonwritingprettyprinted error:nil];     nsstring *jsonstring = [[nsstring alloc] initwithdata:jsondata2 encoding:nsutf8stringencoding];       post = [nsstring stringwithformat:@"message=%@", jsonstring];      nslog(@"post data: \n%@", post);     nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes];     nsstring *postlength = [nsstring stringwithformat:@"%d", [postdata length]];     nsurl *logurl = [nsurl urlwithstring:@"testphp.php"];     nsmutableurlrequest *logrequest = [nsmutableurlrequest requestwithurl:logurl];     [logrequest sethttpmethod:@"post"];     [logrequest setvalue:postlength forhttpheaderfield:@"content-length"];     [logrequest setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"];     [logrequest sethttpbody:postdata];     nsurlconnection *logconnection = [[nsurlconnection alloc] initwithrequest:logrequest delegate:self]; 

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

textfile

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

if explain i'm doing wrong great. thank in advanced.

the problem string isn't valid json string. if modify string this, should work:

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

you need adapt code bit because structure bit different.

also, when want validate json file, can use jsonlint.


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 -