안녕하세요, 아래 예제와 같이 여러 개의 json 객체를 사용하려고합니다. 이이 보고서 로깅 시스템입니다 만이 필요한 경우 메시지가 전달 될 필요가 있기 때문에 내가 한 번에 개체 중 하나를 만들 수 있기 때문에이 아이폰 OS 측에서 만들어지는 방법입니다텍스트 파일의 여러 JSON 객체
[
{
"DateandTime" : "1025",
"LoggingLevel" : "ERROR",
"Description" : "Test"
}
]
[
{
"DateandTime" : "1025",
"LoggingLevel" : "ERROR",
"Description" : "Test"
}
]
. 따라서 Json 객체는 별도의 시간에 만들어지고 파일에 추가됩니다.
유효한 Json 문자열은 다음과 같이 보입니다.
[
{
"DateandTime" : "1025",
"LoggingLevel" : "ERROR",
"Description" : "Test"
},
{
"DateandTime" : "1025",
"LoggingLevel" : "ERROR",
"Description" : "Test"
}
]
그러나 내가 원하는 것은 아닙니다. 두 개의 분리 된 Json 객체를 사용하는 방법이 있습니까?
아이폰 OS
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 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);
});
});
}
파일의 JSON 개체가 이미있는 경우 누군가가 내가 함께 객체를 병합 할 수있는 방법 저를 보여줄 수 또는 주변에 다른 작업이 있습니까?
감사합니다.
그것.파서 측에서 몇 가지 해결 방법을 사용하여 제대로 작동 할 수 있다고 확신합니다. 그런데 처음부터 적절한 JSON을 작성하는 데 어떤 노력을 기울이시겠습니까? –
마찬가지로 @RonniEgeriis는 새로운 내용을 추가 한 다음 json 문자열이 있으면이 파일을 구문 분석 할 방법을 찾는 대신 해당 파일에 쓰는 것이 훨씬 쉽습니다. –
@RonniEgeriis 내가 설명한 것처럼 하나의 객체 만 유효한 JSON입니다. 그러나 나는 동시에 모든 JSON 데이터를 보내지는 않을 것입니다. 보고서를 로깅하고 텍스트 파일에 추가하기 때문에 여러 번 전송됩니다. 누군가가 두 객체를 함께 추가하는 방법이나 주위에 다른 방법을 보여줄 수 있는지 묻는 질문을 업데이트했습니다. –