您好我面临操纵json结构的问题。我是gson的初学者。我从IoT平台获得了一个复杂的json结构,我可以获得结构的第一级(tenantid,timestamp)的值,但是当我想深入到结构时它会崩溃。我没有找到帮助元素进入gson文档来完成这个有效负载。我试图将一个类映射到它 - 它没有用,可能对我来说太复杂了...... 谢谢你的帮助!
/* The payload structure is as follow :
* {"payload":
* "{
* \"tenantId\":\"56ab3a090cf2f00fce9ad9\",
* \"timestamp\":\"2017-07-15T10:56:27.124Z\",
* \"firingRule\":
* {
* \"id\":\"0b4e0dcd-4634-4f2-96ce-7ceefd95b584\",
* \"name\":\"testFR0StreamSample02-01\",
* \"enabled\":true,
* \"matchingRuleIds\": [\"e57927-be87-48dd-be42-8c01d13004d0\"],
* \"aggregationKeys\": [\"metadata.source\"],
* \"firingType\":\"ALWAYS\"
* },
* \"matchingContext\":
* {
* \"tenantId\":\"56ab3a090cf600fce9ad9\",
* \"timestamp\":\"2017-07-15T10:56:27.114Z\",
* \"matchingRule\":
* {
* \"id\":\"e5ec7927-be7-48dd-be42-8c01d13004d0\",
* \"name\":\"Test temperature > 20\",
* \"enabled\":true,
* \"dataPredicate\":
* {
* \">\":[{\"var\":\"value.temperature\"},20]}},
* \"data\":
* {
* \"streamId\":\"android35739073120059\",
* \"timestamp\":\"2017-07-15T10:56:27.106Z\",
* \"location\":{\"lat\":48.872015,\"lon\":2.348264},
* \"model\":\"ModelOABDemoApp00\",
* \"value\":
* {
* \"revmin\":7505,
* \"hygrometry\":98,
* \"temperature\":92},
* \"tags\":[\"OABDemoApp.00\"],
* \"metadata\":
* {
* \"source\":\"URN:LO:NSID:SENSOR:TESTFLGAPPOAB00000\",
* \"connector\":\"mqtt\"
* }
* }
* }
* }"}
*/
//这是我的代码的摘录
Gson gson = new Gson();
String sLiveObjectsPayload = "{\"tenantId\":\"56ab3a090cf2ff600fce9ad9\",\"timestamp\":\"2017-07-29T08:31:24.026Z\",\"firingRule\":{\"id\":\"0b4e0dcd-4634-4ef2-96ce-7ceefd95b584\",\"name\":\"testFR0StreamSample02-01\",\"enabled\":true,\"matchingRuleIds\":[\"e5ec7927-be87-48dd-be42-8c01d13004d0\"],\"aggregationKeys\":[\"metadata.source\"],\"firingType\":\"ALWAYS\"},\"matchingContext\":{\"tenantId\":\"56ab3a090cf2ff600fce9ad9\",\"timestamp\":\"2017-07-29T08:31:24.012Z\",\"matchingRule\":{\"id\":\"e5ec7927-be87-48dd-be42-8c01d13004d0\",\"name\":\"Test temperature > 20\",\"enabled\":true,\"dataPredicate\":{\">\":[{\"var\":\"value.temperature\"},20]}},\"data\":{\"streamId\":\"android357329073120059\",\"timestamp\":\"2017-07-29T08:31:23.992Z\",\"location\":{\"lat\":48.872015,\"lon\":2.348264},\"model\":\"ModelOABDemoApp00\",\"value\":{\"revmin\":9147,\"hygrometry\":70,\"temperature\":118},\"tags\":[\"OABDemoApp.00\"],\"metadata\":{\"source\":\"URN:LO:NSID:SENSOR:TESTFLGAPPOAB00000\",\"connector\":\"mqtt\"}}}}";
JsonObject jsonPayload ;
jsonPayload = gson.fromJson(sLiveObjectsPayload, JsonObject.class);
String sTenantId ;
String sTimeStamp ;
String sFiringRule ;
String sFiringRuleId ;
String sName ;
sTenantId = jsonPayload.get("tenantId").getAsString(); // ok
sTimeStamp = jsonPayload.get("timestamp").getAsString(); // ok
sFiringRuleId = jsonPayload.get("firingRule.id").getAsString(); // crash
sFiringRule = jsonPayload.get("firingRule").getAsString(); // crash
sName = jsonPayload.get("name").getAsString(); // crash
答案 0 :(得分:0)
您可以使用getAsJsonObject()
访问子JSON对象。
sTenantId = jsonPayload.get("tenantId").getAsString();
sTimeStamp = jsonPayload.get("timestamp").getAsString();
JsonObject firingRule = jsonPayload.getAsJsonObject("firingRule");
sFiringRuleId = firingRule.get("id").getAsString();
sName = firingRule.get("name").getAsString();