如何访问字符串列表中的字典?

时间:2018-07-17 17:28:00

标签: python string list dictionary

value = "[
{"Id":2,"Name":"Sony","WarehouseName":"Bangalore"},
{"Id":3,"Name":"JBL","WarehouseName":"Nanjangud"},
{"Id":4,"Name":"Skull Candy 2","WarehouseName":"Mysore"},
{"Id":5,"Name":"Skull Candy 3","WarehouseName":"Mysore"}
]"

这是包含列表的字符串,其中有一个字典。如何获得“名称”:

索尼

JBL

骷髅糖果2

骷髅糖果3

2 个答案:

答案 0 :(得分:1)

您可以为此使用Caused by: java.lang.NoClassDefFoundError: org/wso2/carbon/apimgt/micro/gateway/usage/publisher/util/UsagePublisherException at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) at java.lang.Class.getConstructor0(Class.java:3075) at java.lang.Class.getDeclaredConstructor(Class.java:2178) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory .java:1032) ... 135 more Caused by: java.lang.ClassNotFoundException: org.wso2.carbon.apimgt.micro.gateway.usage.publisher.util.UsagePublisherException at org.wso2.carbon.webapp.mgt.l 模块

TID: [-1234] []  ERROR {org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher} -  Error while publishing throttling events to global policy server {org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher}
java.lang.NullPointerException
        at org.wso2.carbon.apimgt.gateway.throttling.publisher.ThrottleDataPublisher.publishNonThrottledEvent(ThrottleDataPublisher.java:121)
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.doRoleBasedAccessThrottlingWithCEP(ThrottleHandler.java:349)
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.doThrottle(ThrottleHandler.java:512)
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.handleRequest(ThrottleHandler.java:460)
        at org.apache.synapse.rest.API.process(API.java:325)
        at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:90)
        at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:69)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:303)
        at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:92)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:337)
        at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:158)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

获取所有名称

json

答案 1 :(得分:0)

您可以使用public class Main extends Application { @Override public void start(Stage primaryStage) { try { //BorderPane root = new BorderPane(); Parent root = FXMLLoader.load(getClass().getResource("/fxml/Contrlpanel.FXML")); Scene scene = new Scene(root,487,406); scene.getStylesheets().add(getClass().getResource("/fxml/application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); }} json模块。

例如:

ast

import json
value = """[{"Id":2,"Name":"Sony","WarehouseName":"Bangalore"},{"Id":3,"Name":"JBL","WarehouseName":"Nanjangud"},{"Id":4,"Name":"Skull Candy 2","WarehouseName":"Mysore"},{"Id":5,"Name":"Skull Candy 3","WarehouseName":"Mysore"}]"""
print( json.loads(value) )

输出:

import ast
value = """[{"Id":2,"Name":"Sony","WarehouseName":"Bangalore"},{"Id":3,"Name":"JBL","WarehouseName":"Nanjangud"},{"Id":4,"Name":"Skull Candy 2","WarehouseName":"Mysore"},{"Id":5,"Name":"Skull Candy 3","WarehouseName":"Mysore"}]"""
print( ast.literal_eval(value) )

print( map(lambda x: x["Name"], ast.literal_eval(value)) )