2016-11-08 7 views
-1

以下のサンプルJSONがあります。私はASIdentiferExternalIdentiferのような個々のフィールドを取得する必要があります。このJSONデータを文字列に保存しました。モジュールとして(ggson)フィールド値を取得するためにggsonを使用してJSON文字列を解析する方法

JSONデータをGoogleJsonを使用 :私はPOJOクラスを作成したコードの下に使用してデータを解析さ

{ 
     "DeviceCommon": { 
      "ASIdentifier": "123", 
      "DatadeliveyMechanism": "notify", 
      "MobileOriginatorCallbackReference": { 
       "url": "http://application.example.com/inbound/notifications/modatanotification/" 
      }, 
      "AccessiblityCallbackReference": { 
       "url": "http://application.example.com/inbound/notifications/accessibilitystatusnotification" 
      } 
     }, 
     "DeviceList": [{ 
      "ExternalIdentifer": "[email protected]", 
      "msisdn": "123456", 
      "senderName": "Device1", 
      "MobileOriginatorCallbackReference": { 
       "notifyURL": "http://application.example.com/inbound/notifications/modatanotification/" 
      }, 
      "ConfigurationResultCallbackReference": { 
       "notifyURL": "http://application.example.com/inbound/notifications/configurationResult" 
      }, 
      "ASreferenceID": "AS000001", 
      "NIDDduration": "1d" 
     }] 
    } 

data = new Gson().fromJson(new FileReader("/home/raj/apache-tomcat-8.0.3/webapps/file.json"), Data.class); 
System.out.println(data); 

出力:

Data{ 
    deviceCommon=DeviceCommon{ 
    asIdentifier='123' 
    datadeliveyMechanism='notify' 
    mobileOriginatorCallbackReference=http://application.example.com/inbound/notifications/modatanotification/ 
    accessiblityCallbackReference=http://application.example.com/inbound/notifications/accessibilitystatusnotification 
    } 
    deviceList=[DeviceListEntry{ 
    externalIdentifer='[email protected]' 
    msisdn='123456' 
    senderName='Device1' 
    mobileOriginatorCallbackReference=http://application.example.com/inbound/notifications/modatanotification/ 
    configurationResultCallbackReference=http://application.example.com/inbound/notifications/configurationResult 
    asReferenceID='AS000001' 
    nidDduration='1d' 
    }] 
} 

String jsonInString = gson.toJson(data); 
System.out.println("String is"+ jsonInString); 

出力:

String is{"DeviceCommon":{"ASIdentifier":"123","DatadeliveyMechanism":"notify","MobileOriginatorCallbackReference":{"url":"http://application.example.com/inbound/notifications/modatanotification/"},"AccessiblityCallbackReference":{"url":"http://application.example.com/inbound/notifications/accessibilitystatusnotification"}},"DeviceList":[{"ExternalIdentifer":"[email protected]","msisdn":"123456","senderName":"Device1","MobileOriginatorCallbackReference":{"notifyURL":"http://application.example.com/inbound/notifications/modatanotification/"},"ConfigurationResultCallbackReference":{"notifyURL":"http://application.example.com/inbound/notifications/configurationResult"},"ASreferenceID":"AS000001","NIDDduration":"1d"}]} 

私はExternalIdentifierASIdentifierなどの個々のフィールドを取得するには、このJSON文字列を解析する必要があります。

私はこれを試しましたが、動作しません。

JsonObject jobj = new Gson().fromJson(jsonInString, JsonObject.class); 
String result = jobj.get("ASIdentifier").toString(); 
System.out.println("value is"+ result); 

注:ExternalIdentifierは、アレイ内にあるので、私はそれを見つけるために、配列をループする必要があります。

私が間違っていることを教えてもらえますか?

答えて

0

考えられる解決策:

String result = jobj.get("DeviceCommon").getAsJsonObject().get("ASIdentifier").getAsString(); 
System.out.println("ASIdentifier: "+ result); 

JsonArray jsonArray = jobj.get("DeviceList").getAsJsonArray(); 
for (JsonElement device : jsonArray) { 
    result = device.getAsJsonObject().get("ExternalIdentifer").getAsString(); 
    System.out.println("ExternalIdentifer: "+ result); 
} 

出力:

ASIdentifier:123

ExternalIdentifer:[email protected]

+0

感謝。私は以下のように価値を得ることができた – ramkriz

0
public static void printJson(JsonElement jsonElement,String key) { 

      // Check whether jsonElement is JsonObject or not 
      if (jsonElement.isJsonObject()) { 
       Set<Entry<String, JsonElement>> ens = ((JsonObject) jsonElement).entrySet(); 
       if (ens != null) { 
        // Iterate JSON Elements with Key values 
        for (Entry<String, JsonElement> en : ens) { 
         // System.out.println("##key is"+en.getKey() + " : "); 
         printJson(en.getValue(), en.getKey()); 
         // System.out.println(en.getValue().getAsString()); 
         // System.out.println(jsonElement.getAsString()); 
        } 
       } 
      } 
     // Check whether jsonElement is Primitive or not 
      else if (jsonElement.isJsonPrimitive()) { 
       // print value as String 
       System.out.println("###key is"+key); 
       System.out.println("### value is"+jsonElement.getAsString()); 
      } 

      else if (jsonElement.isJsonArray()) { 
       JsonArray jarr = jsonElement.getAsJsonArray(); 
       // Iterate JSON Array to JSON Elements 
       System.out.println("\n###Array size is"+ jarr.size()); 
       for (JsonElement je : jarr) { 
        printJson(je,key); 
       } 
    } 


    } 
関連する問題