自定义JSON格式

2024-09-29 21:29:12 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将下一行(不确定这是什么格式)转换成JSON格式?

[root=Root [key1=value1, key2=value2, key3=Key3 [key3_1=value3_1, key3_2=value3_2, key3_3=Key3_3 [key3_3_1=value3_3_1]], key4=value4]]

其中RootKey3Key3_3表示复杂元素。你知道吗

{
        "root": {
                "key1" : "value1",
                "key2" : "value2",
                "key3" : {
                        "key3_1" : "value3_1",
                        "key3_2" : "value3_2",
                        "key3_3" : {
                                "key3_3_1" : "value3_3_1"
                        }
                },
                "key4" : "value4
        }
}

我在寻找方法,而不是解决方案。如果你对这个问题投反对票,请说明你为什么这么做。你知道吗


Tags: 方法json元素格式rootkey2key1value1
2条回答

我不得不花了大约两个小时在这个问题上,但我想我有一些东西,可以工作的所有案件的基础上,你提供的格式。如果没有,我相信这将是一个小的变化。尽管您只要求提供这个想法,但由于我编写了代码,这里是Python代码。你知道吗

import json

def to_json(cust_str):
    from_index = 0
    left_indices = []
    levels = {}

    level = 0
    for i, char in enumerate(cust_str):
        if char == '[':
            level += 1
            left_indices.append(i)
            if level in levels:
                levels[level] += 1
            else:
                levels[level] = 1
        elif char == ']':
            level -= 1

    level = max(levels.keys())
    value_stack = []
    while True:
        left_index = left_indices.pop()
        right_index = cust_str.find(']', left_index) + 1
        values = {}
        pairs = cust_str[left_index:right_index][1:-1].split(',')

        if levels[level] > 0:
            for pair in pairs:
                pair = pair.split('=')
                values[pair[0].strip()] = pair[1]
        else:
            level -= 1
            for pair in pairs:
                pair = pair.split('=')
                if pair[1][-1] == ' ':
                    values[pair[0].strip()] = value_stack.pop()
                else:
                    values[pair[0].strip()] = pair[1]
        value_stack.append(values)
        levels[level] -= 1
        cust_str = cust_str[:left_index] + cust_str[right_index:]

        if levels[1] == 0:
            return json.dumps(values)

if __name__ == '__main__':
    # Data in custom format
    cust_str = '[root=Root [key1=value1, key2=value2, key3=Key3 [key3_1=value3_1, key3_2=value3_2, key3_3=Key3_3 [key3_3_1=value3_3_1]], key4=value4]]'
    # Data in JSON format
    json_str = to_json(cust_str)
    print json_str

其思想是,我们将dict以自定义格式映射到的级别数,以及不是与这些级别对应的字符串的值数。同时,我们跟踪给定字符串中[字符的索引。然后,我们从最里面的dict表示开始,弹出包含[(左)索引的堆栈并解析它们。在解析它们时,我们将它们从字符串中移除并继续。剩下的你可以在代码里读到。你知道吗

我根据你提供的数据运行了它,结果如下。你知道吗

{
   "root":{
      "key2":"value2",
      "key3":{
         "key3_2":"value3_2",
         "key3_3":{
            "key3_3_1":"value3_3_1"
         },
         "key3_1":"value3_1"
      },
      "key1":"value1",
      "key4":"value4"
   }    
}

为了确保它适用于更一般的情况,我使用了这个自定义字符串。你知道吗

[root=Root [key1=value1, key2=Key2 [key2_1=value2_1], key3=Key3 [key3_1=value3_1, key3_2=Key3_2 [key3_2_1=value3_2_1], key3_3=Key3_3 [key3_3_1=value3_3_1]], key4=value4]]

并分析了它。你知道吗

{
   "root":{
      "key2":{
         "key2_1":"value2_1"
      },
      "key3":{
         "key3_2":{
            "key3_2_1":"value3_2_1"
         },
         "key3_3":{
            "key3_3_1":"value3_3_1"
         },
         "key3_1":"value3_1"
      },
      "key1":"value1",
      "key4":"value4"
   } 
}

据我所知,这就是它应该被解析的方式。另外,请记住,不要剥离这些值,因为逻辑依赖于值末尾的空格,这些值应该以dict作为值(如果这有意义的话)。你知道吗

x为具有上述序列化的字符串。你知道吗

首先,让我们用空字符串替换出现的RootKey3Key3_3

# the string fragments like "root=Root [" need to be replaced by "root=["
# to achieve this, we match the regex pattern "\w+ ["
# This matches ALL instances in the input string where we have a word bounded by "=" & " [",
# i.e. "Root [", "Key3 [", "Key3_3" are all matched. as will any other example you can think of 
# where the `word` is composed of letters numbers or underscore followed
# by a single space character and then "["
# We replace this fragment with "[", (which we will later replace with "{")
# giving us the transformation "root=Root [" => "root=["
import re
o = re.compile(r'\w+ [[]')
y = re.sub(o, '[', x, 0)

然后,让我们将结果字符串拆分为单词和非单词

# Here we split the string into two lists, one containing adjacent tokens (nonwords)
# and the other containing the words
# The idea is to split / recombine the source string with quotes around all our words

w = re.compile(r'\W+')
nw = re.compile(r'\w+')

words = w.split(y)[1:-1] # ignore the end elements which are empty.
nonwords = nw.split(y) # list elements are contiguous non-word characters, i.e not a-Z_0-9
struct = '"{}"'.join(nonwords) # format structure of final output with quotes around the word's placeholder.
almost_there = struct.format(*words) # insert words into the string

最后,用扭曲的方括号替换方括号,用=替换:

jeeson = almost_there.replace(']', '}').replace('=', ':').replace('[', '{')
# "{'root':{'key1':'value1', 'key2':'value2', 'key3':{'key3_1':'value3_1', 'key3_2':'value3_2', 'key3_3':{'key3_3_1':'value3_3_1'}}, 'key4':'value4'}}"

相关问题 更多 >

    热门问题