2017-07-29 47 views
0

嗨我正在面臨操作json結構的問題。我是gson的初學者。我從物聯網平臺獲得複雜的json結構,我可以獲得結構第一級的值(tenantid,timestamp),但是當我想深入到結構中時,它會崩潰。我還沒有找到幫助元素到gson文檔來通過這個有效載荷。我試圖將一個類映射到它 - 它不工作,可能對我來說太複雜了...... 感謝您的幫助!json與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

您可以訪問使用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(); 
+0

謝謝,效果很好! – fragae