我有一个以
响应的端点{
"fruits" : {
"apple" : "green",
"banana" : 99,
"oranges" : "floridaBreed",
"lemons" : "sicilian",
"grapes" : "red",
"cherries" : 12,
"guava" : "sour",
"tomato" : 13,
"melons" : "yellow",
"pomegranate" : 37,
},
"isRaw" : null,
"foodtype" : {
"typeOne" : "non-veg",
"typeTwo" : "veg"
},
"isCooked" : [ ],
"isReady" : [ ],
"isExpired" : [ ],
"isHealthy" : null,
"serialnumber" : 5555,
"isAvailable" : "Yes",
"dietValue" : {
"nutrition" : [ {
"vitamin" : "yes",
"vitaminValue" : 3
}, {
"calcium" : "no",
"calciumValue" : 0
} ]
},
"isEdible" : null
}
我有一些代码可以解决这个问题,我无法获得嵌套值对象。例如,我可以获得' isAvailable'这是='是'但如果我想获得葡萄的价值。或者对于钙,然后它不起作用。有什么想法吗?
public String sendGetMessageValue(String messageId) {
Map<String, String> response = doInternalModuleApiGet(messageId, 200);
return response.get("fruits.grapes");
}
public Map<String, String> doInternalModuleApiGet(
String messageId,
int expectedStatus) {
String url = getInternalUrl() + "/" + messageId;
return sendHttpGetRequestAndGetResponse(url, expectedStatus).as(Map.class);
}
private ResponseBodyExtractionOptions sendHttpGetRequestAndGetResponse(String url, int expectedStatus) {
return given()
.header("Authorization", "basic " + B64Code.encode("dummyuser:dummypass"))
.get(url)
.then()
.log().ifError().and()
.assertThat().statusCode(equalTo(expectedStatus)).and().extract().body();
}
public String getInternalUrl() {
return ("http://127.0.0.1:8080/api");
}
答案 0 :(得分:0)
org.json库易于使用。示例代码如下:
import uasyncio as asyncio
from machine import Pin
import time
LED_PIN = 13
led = Pin(LED_PIN, Pin.OUT, value=1)
async def toggle():
while True:
await asyncio.sleep_ms(500)
led.value(not led.value()) # toggling
async def webServer(ipAddress):
s = socket.socket()
ai = socket.getaddrinfo(ipAddress, 8080)
print("Bind address info:", ai)
addr = ai[0][-1]
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(2)
print("Listening, connect your APP to http://%s:8080/" % ipAddress)
counter = 0
# await asyncio.sleep(20) # !! if i applied await here, LED toggling 20 secs but web server does not accept any request because "while True" below is not activated during 20 secs.
while True:
res = s.accept()
client_sock = res[0]
client_addr = res[1]
print("Client address:", client_addr)
print("Client socket:", client_sock)
req = client_sock.recv(1024)
print("Payload: %s" % req.decode())
client_sock.send(CONTENT % counter)
client_sock.close()
counter += 1
print()
loop = asyncio.get_event_loop()
loop.create_task(toggle())
loop.create_task(webServer('192.168.4.1'))
loop.run_forever()
您可以通过修改以下代码来获取此详细信息:
JSONObject completeJSON= new JSONObject(" ..YOUR JSON String Goes Here.. ");
String grapes= completeJSON.getJSONObject("fruits").getString("grapes");//Level2
String available= completeJSON.getString("isAvailable"); //Level1
中找到更多示例