如果config有多个值,如何制作json数组?

时间:2018-05-16 12:51:19

标签: python json

我需要根据配置值创建正确的Json。例如,如果config具有下一个键值:channel_id = 1,2,3,4我需要得到json:channel_id" : [1,2,3,4],但现在我得到"channel_id" : "1,2,3,4"

myfilter = dict(
        client_id=config['client_id'], 
        channel_id=config['channel_id']
        )
x = json.dumps(myfilter)

1 个答案:

答案 0 :(得分:1)

您需要首先将配置值解析/拆分为public void run() { if (!isLoading && !playerList.isEmpty()) { this.isLoading = true; ArrayList<Player> clonedList = (ArrayList<Player>)playerList.clone(); playerList.clear(); try { for (Player player : clonedList) { main.getDataManager().loadPlayer(player); } } catch (Exception e) { } this.isLoading = false; } }

list

鉴于这将产生字符串,您可能希望首先将它们转换为整数(即​​myfilter = dict( client_id=config['client_id'], channel_id=[x.strip() for x in config['channel_id'].split(",")] # split and strip ) 而不是int(x))。