简体   繁体   English

使用Python将JSONObject转换为JSONArray

[英]Convert JSONObject into JSONArray using Python

I have gone through various threads, but couldn't find the particular answer in python. 我经历了各种各样的话题,但是在python中找不到特定的答案。 I have a json file 我有一个json文件

{
    "StoreID" : "123",
    "Status" : 3,
    "data" : {
            "Response" : {
                    "section" : "25",
                    "elapsed" : 277.141,
                    "products" : {
                            "prd_1": {
                                    "price" : 11.99,
                                    "qty" : 10,
                                    "upc" : "0787493"
                            },
                            "prd_2": {
                                    "price" : 9.99,
                                    "qty" : 2,
                                    "upc" : "0763776"
                            },
                            "prd_3": {
                                    "price" : 29.99,
                                    "qty" : 8,
                                    "upc" : "9948755"
                            }
                    },
                    "type" : "Tagged"
            }
    }
}

I need to convert this json file into the format below, by changing json object 'products' into an array form. 我需要通过将json对象'products'更改为数组形式,将此json文件转换为以下格式。

{
    "StoreID" : "123",
    "Status" : 3,
    "data" : {
            "Response" : {
                    "section" : "25",
                    "elapsed" : 277.141,
                    "products" : [
                            {
                                    "price" : 11.99,
                                    "qty" : 10,
                                    "upc" : "0787493"
                            },
                            {
                                    "price" : 9.99,
                                    "qty" : 2,
                                    "upc" : "0763776"
                            },
                            {
                                    "price" : 29.99,
                                    "qty" : 8,
                                    "upc" : "9948755"
                            }
                    ],
                    "type" : "Tagged"
            }
    }
}

Is there any good way to do it in python. 有什么好的方法可以在python中做到这一点。 Mostly I saw people are using java, but not in python. 通常,我看到人们在使用Java,但在python中却没有。 Can you please let me know a way to do it in python. 您能否让我知道在python中执行此操作的方法。

Just get the values() of products dictionary and that will give you an array of values. 只需获取产品字典的values(),它将为您提供一个值数组。 Code below works from me assuming your json is in file1.txt Also note 假设您的json位于file1.txt中,以下代码对我有效

import json
with open('file1.txt') as jdata:
    data = json.load(jdata)
    d = data
d["data"]["Response"]["products"] = d["data"]["Response"]["products"].values()
print(json.dumps(d))

output: 输出:

{"Status": 3, "StoreID": "123", "data": {"type": "Tagged", "Response": {"section": "25", "products": [{"price": 9.99, "upc": "0763776", "qty": 2}, {"price": 29.99, "upc": "9948755", "qty": 8}, {"price": 11.99, "upc": "0787493", "qty": 10}], "elapsed": "277.141"}}}

Would something like this work for you? 这样的事情对您有用吗?

import json
import copy

a = json.load(open("your_data.json", "r"))

b = copy.deepcopy(a)

t =  a.get('data').get('Response').get('products')
b['data']['Response']['products'] = t.values()    # Originally was: [t[i] for i in t]

You can give back JSON with json.dumps(b) 您可以使用json.dumps(b)来返回JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM