[英]PHP - Convert data to valid json
I have a huge .txt file with data in this format: 我有一个巨大的.txt文件,其数据格式如下:
{
"template_id": "prefix_1",
"settings": {
"setting1": "bla",
"setting2": "blub"
},
"test": 1,
"test": 2,
"test": 3
},
{
"template_id": "other_prefix_1",
"values": {
"value1": "bla",
"value2": "blub"
},
"something": "Hello!"
},
{
"template_id": "prefix_2",
"settings": {
"setting1": "bla",
"setting2": "blub"
},
"test": 1,
"test": 2,
"test": 3
}
That looks like json, but it is not valid json and I want to make it valid. 看起来像json,但它不是有效的json,我想使其有效。 The result should be something like this:
结果应该是这样的:
{
"prefix": [
{
"id": "1",
"settings": {
"setting1": "bla",
"setting2": "blub"
},
"tests": [
"test": 1,
"test": 2,
"test": 3
]
},
{
"id": "2",
"settings": {
"setting1": "bla",
"setting2": "blub"
},
"tests": [
"test": 1,
"test": 2,
"test": 3
]
}
],
"other_prefix": [
{
"id": "1",
"values": {
"value1": "bla",
"value2": "blub"
},
"something": "Hello!"
}
]
}
Someone have a good idea how I can do that with PHP, when I read the data from the txt file? 当我从txt文件读取数据时,有人对我如何用PHP做到这一点有一个好主意? The lines are exactly like this, so if necessary I can read every line.
这些行正是这样,因此,如有必要,我可以阅读每一行。
Unless the format that you have in the TXT file is a real format that can be parsed by a PHP function into an object, I am afraid that you will need to write a parser from scratch. 除非TXT文件中的格式是可以由PHP函数解析为对象的真实格式,否则恐怕您将需要从头开始编写解析器。 It will involve a loop that read line by line, some IFs statments and regular expressions.
它将涉及一个循环,逐行读取一些IF语句和正则表达式。
Then you can build a hash containing hashes and lists. 然后,您可以构建包含哈希和列表的哈希。
And then you can convert this hash to a JSON string using json_encode($myhash). 然后,您可以使用json_encode($ myhash)将此哈希转换为JSON字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.