需要PYTHON JSONtoDICT帮助-似乎python会将我的json-字典转换解释为列表

时间:2018-07-28 20:45:01

标签: python json dictionary

以下代码给了我错误:

public class Have extends AppCompatActivity {

private Button HaveBack;
private TextView Have;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.have);
    HaveBack = (Button) findViewById(R.id.HaveBack);
    Have= (TextView) findViewById(R.id.Have);
    String Text_Value= TextAdd.License.getText().toString();
    String Extra = Text_Value + " is already part of Your license plates";
    Have.setText(Extra);
    HaveBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.Text_Checker);
        }
    });
}
}

代码:

Traceback (most recent call last):   File "AMZGetPendingOrders.py", line 66, in <module>
    item_list.append(item['SellerSKU']) TypeError: string indices must be integers

我不明白为什么Python认为这是列表而不是字典。当我打印from mws import mws import time import json import xmltodict access_key = 'xx' #replace with your access key seller_id = 'yy' #replace with your seller id secret_key = 'zz' #replace with your secret key marketplace_usa = '00' orders_api = mws.Orders(access_key, secret_key, seller_id) orders = orders_api.list_orders(marketplaceids=[marketplace_usa], orderstatus=('Pending'), fulfillment_channels=('MFN'), created_after='2018-07-01') #save as XML file filename = 'c:order.xml' with open(filename, 'w') as f: f.write(orders.original) #ConvertXML to JSON dictString = json.dumps(xmltodict.parse(orders.original)) #Write new JSON to file with open("output.json", 'w') as f: f.write(dictString) #Read JSON and parse our order number with open('output.json', 'r') as jsonfile: data = json.load(jsonfile) #initialize blank dictionary id_list = [] for order in data['ListOrdersResponse']['ListOrdersResult']['Orders']['Order']: id_list.append(order['AmazonOrderId']) #This "gets" the orderitem info - this code actually is similar to the initial Amazon "get" though it has fewer switches orders_api = mws.Orders(access_key, secret_key, seller_id) #opens and empties the orderitem.xml file open('c:orderitem.xml', 'w').close() #iterated through the list of AmazonOrderIds and writes the item information to orderitem.xml for x in id_list: orders = orders_api.list_order_items(amazon_order_id = x) filename = 'c:orderitem.xml' with open(filename, 'a') as f: f.write(orders.original) #ConvertXML to JSON amz_items_pending = json.dumps(xmltodict.parse(orders.original)) #Write new JSON to file with open("pending.json", 'w') as f: f.write(amz_items_pending) #read JSON and parse item_no and qty with open('pending.json', 'r') as jsonfile1: data1 = json.load(jsonfile1) #initialize blank dictionary item_list = [] for item in data1['ListOrderItemsResponse']['ListOrderItemsResult']['OrderItems']['OrderItem']: item_list.append(item['SellerSKU']) #print(item) #print(id_list) #print(data1) #print(item_list) time.sleep(10) 时,它看起来像字典(花括号,单引号,冒号等)

id_list显示了我的字典

print(data1)

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

因为您要遍历dict中的每个键值:

{'QuantityOrdered ': '1', 'Title': 'Delta Rothko Rolling Bicycle Stand', 'ConditionId': 'New', 'Is Gift': 'false', 'ASIN': 'B00XXXXTIK', 'SellerSKU': '9934638', 'OrderItemId': '49 624373726506', 'ProductInfo': {'NumberOfItems': '1'}, 'QuantityShipped': '0', 'C onditionSubtypeId': 'New'}

因此item中的第一个值将是'QuantityOrdered',并且您试图像访问字典一样访问此字符串

您可以这样做:

id_list.append(data1['ListOrderItemsResponse']['ListOrderItemsResult']['OrderItems']['OrderItem']['SellerSKU']))

并避免字典中的for循环

答案 1 :(得分:0)

我猜您正在尝试迭代OrderItems并查找其SellerSKU值。

DATEPART(dw, DateField)