Зосимов,
а переписать нельзя, без добавления? |
Цитата:
|
в общем заморочка получается с правильным json -надо считать файл, записать заново с новыми данными. не удобно и наверное долго?
может все таки оставить в том формате что было? и как перебирать как массив например? |
Цитата:
|
Цитата:
|
Цитата:
Цитата:
|
Alexandroppolus,
ок :)
var data = `{"name":"John","lastName":"Doe","age":35,"gende r":"Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
`;
data = JSON.parse("["+data.replace(/}\s+{/g, "},{")+"]");
alert(data.map(function(item) {
return item.name + ' | ' + item.lastName;
}).join('\n'));
|
Зосимов,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
ul{
list-style: decimal;
}
</style>
</head>
<body>
<ul id="info"></ul>
<script>
var data = `{"name":"John","lastName":"Doe","age":35,"gender":"Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
`;
data = JSON.parse("["+data.replace(/}\s+{/g, "},{")+"]");
var html = data.reduce(function(temp, item) {
return temp + '<li>' + Object.values(item).join(" | ") + '</li>';
},"");
info.innerHTML = html;
</script>
</body>
</html>
|
| Часовой пояс GMT +3, время: 13:37. |