从文本文件加载 Json 对象 - 返回未捕获的语法错误:意外标记“[ ”


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

我在从文本文件中获取我的 Json 对象并能够在 javascript 函数中使用它们时遇到问题。但是,如果只有一个 Json 对象,它就可以工作。然后,当我开始拥有多个 Json 对象时,它会给我错误:Uncaught SyntaxError: Unexpected token [

这是我下面的代码:

苹果

    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 the file to get existing content
    $current = file_get_contents($file);
    if (isset($_POST['Message'])) {
    // Append a new person to the file
    $current .= $_POST['Message'] . PHP_EOL; 
    // Write the contents back to the file
    file_put_contents($file, $current);
    } else {
        $Contents = file_get_contents($file);
        echo $Contents;
    }

爪哇语

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

文本文件

   [
     {
        "DateandTime" : "1025",
        "LoggingLevel" : "ERROR",
        "Description" : "Test"
     }
   ]
   [
     {
       "DateandTime" : "1025",
       "LoggingLevel" : "ERROR",
       "Description" : "Test"
     }
   ]

如果有人能解释我做错了什么,那就太好了。提前谢谢你。

问题是您的字符串不是有效的 Json 字符串。如果你像这样修改你的字符串,它应该可以工作:

[
    {
        "DateandTime": "1025",
        "LoggingLevel": "ERROR",
        "Description": "Test"
    },
    {
        "DateandTime": "1025",
        "LoggingLevel": "ERROR",
        "Description": "Test"
    }
]

还需要稍微调整一下代码,因为您返回的结构有点不同。

此外,当您想要验证 Json 文件时,可以使用 JSonLint。

这就是你制作数组的方式

   [
     {
        "DateandTime" : "1025",
        "LoggingLevel" : "ERROR",
        "Description" : "Test"
     },
     {
       "DateandTime" : "1025",
       "LoggingLevel" : "ERROR",
       "Description" : "Test"
     }
   ]