2014-04-02 113 views
0

我已經使用JSON解析了很多次。但是目前我已經得到了一個具有非常不同結構的解析器。這個JSON響應具有nexted JsonObjects和JsonArrays。如何用Java中的嵌套對象解析JSON

我已經試過傑克遜,GSON但​​無法做到這一點,請別人幫助,在此,

這裏的JSON響應在Java中

{"web-app": { 
    "servlet": [ 
    { 
     "servlet-name": "cofaxCDS", 
     "servlet-class": "org.cofax.cds.CDSServlet", 
     "init-param": { 
     "configGlossary:installationAt": "Philadelphia, PA", 
     "configGlossary:adminEmail": "[email protected]", 
     "configGlossary:poweredBy": "Cofax", 
     "configGlossary:poweredByIcon": "/images/cofax.gif", 
     "configGlossary:staticPath": "/content/static", 
     "templateProcessorClass": "org.cofax.WysiwygTemplate", 
     "templateLoaderClass": "org.cofax.FilesTemplateLoader", 
     "templatePath": "templates", 
     "templateOverridePath": "", 
     "defaultListTemplate": "listTemplate.htm", 
     "defaultFileTemplate": "articleTemplate.htm", 
     "useJSP": false, 
     "jspListTemplate": "listTemplate.jsp", 
     "jspFileTemplate": "articleTemplate.jsp", 
     "cachePackageTagsTrack": 200, 
     "cachePackageTagsStore": 200, 
     "cachePackageTagsRefresh": 60, 
     "cacheTemplatesTrack": 100, 
     "cacheTemplatesStore": 50, 
     "cacheTemplatesRefresh": 15, 
     "cachePagesTrack": 200, 
     "cachePagesStore": 100, 
     "cachePagesRefresh": 10, 
     "cachePagesDirtyRead": 10, 
     "searchEngineListTemplate": "forSearchEnginesList.htm", 
     "searchEngineFileTemplate": "forSearchEngines.htm", 
     "searchEngineRobotsDb": "WEB-INF/robots.db", 
     "useDataStore": true, 
     "dataStoreClass": "org.cofax.SqlDataStore", 
     "redirectionClass": "org.cofax.SqlRedirection", 
     "dataStoreName": "cofax", 
     "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", 
     "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", 
     "dataStoreUser": "sa", 
     "dataStorePassword": "dataStoreTestQuery", 
     "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", 
     "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", 
     "dataStoreInitConns": 10, 
     "dataStoreMaxConns": 100, 
     "dataStoreConnUsageLimit": 100, 
     "dataStoreLogLevel": "debug", 
     "maxUrlLength": 500}}, 
    { 
     "servlet-name": "cofaxEmail", 
     "servlet-class": "org.cofax.cds.EmailServlet", 
     "init-param": { 
     "mailHost": "mail1", 
     "mailHostOverride": "mail2"}}, 
    { 
     "servlet-name": "cofaxAdmin", 
     "servlet-class": "org.cofax.cds.AdminServlet"}, 

    { 
     "servlet-name": "fileServlet", 
     "servlet-class": "org.cofax.cds.FileServlet"}, 
    { 
     "servlet-name": "cofaxTools", 
     "servlet-class": "org.cofax.cms.CofaxToolsServlet", 
     "init-param": { 
     "templatePath": "toolstemplates/", 
     "log": 1, 
     "logLocation": "/usr/local/tomcat/logs/CofaxTools.log", 
     "logMaxSize": "", 
     "dataLog": 1, 
     "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log", 
     "dataLogMaxSize": "", 
     "removePageCache": "/content/admin/remove?cache=pages&id=", 
     "removeTemplateCache": "/content/admin/remove?cache=templates&id=", 
     "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder", 
     "lookInContext": 1, 
     "adminGroupID": 4, 
     "betaServer": true}}], 
    "servlet-mapping": { 
    "cofaxCDS": "/", 
    "cofaxEmail": "/cofaxutil/aemail/*", 
    "cofaxAdmin": "/admin/*", 
    "fileServlet": "/static/*", 
    "cofaxTools": "/tools/*"}, 

    "taglib": { 
    "taglib-uri": "cofax.tld", 
    "taglib-location": "/WEB-INF/tlds/cofax.tld"}}} 
+2

你可以請你分享一些示例代碼,你如何試圖解析json? – cremersstijn

+0

[如何解析Java中的JSON]可能的重複(http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) –

回答

-1

嵌套對象被解析爲 String。所以你必須明確地解析它們。

對不起,這裏是一個使用org.json一些工作示例:

import org.json.*; 

// { "key1": "value1", { "key11": "value11", "key12": 12 } } 
JSONObject jsonObject = new JSONObject("{\"key1\":\"value1\",{\"key11\":\"value11\",\"key12\":12}}"); 
int value12 = jsonObject.getJSONObject("key1").getJSONObject("key12"); 
1

你需要的時候你正在處理使用JsonNode#isObject一(嵌套)對象來檢測。

public static void printAll(JsonNode node) { 
    Iterator<String> fieldNames = node.getFieldNames(); 
    while(fieldNames.hasNext()){ 
     String fieldName = fieldNames.next(); 
     JsonNode fieldValue = node.get(fieldName); 
     if (fieldValue.isObject()) { 
      System.out.println(fieldName + " :"); 
      printAll(fieldValue); 
     } else { 
      String value = fieldValue.asText(); 
      System.out.println(fieldName + " : " + value); 
     } 
    } 
} 
0

如果字段類型設置正確,GSON會正確處理嵌套對象。不需要額外的代碼。看看user guide