[英]How to parse JSON String to java object with jackson?
I am currently having trouble trying to parse this VCAP_SERVICES to java objects. 我目前在尝试将此VCAP_SERVICES解析为Java对象时遇到麻烦。 I do not quite understand how to structure the POJO to allow it to map the values from the json string.
我不太了解如何构造POJO以使其能够映射json字符串中的值。 Can someone please help me structure my pojo so that it is aligns with the json string?
有人可以帮我构造我的pojo,使其与json字符串对齐吗?
I want to create objects for both of the credentials: accessToken... jdbcurl. 我想为两个凭证创建对象:accessToken ... jdbcurl。
VCAP_SERVICES VCAP_SERVICES
"VCAP_SERVICES": {
"user-provided": [
{
"credentials": {
"accessTokenUri": "tokenurl",
"apiUrl": "apiurl",
"clientId": "typeofID",
"clientSecret": "secretAf",
"scope": "none"
},
"syslog_drain_url": "",
"volume_mounts": [],
"label": "user-provided",
"name": "OAuth2",
"tags": []
},
{
"credentials": {
"jdbcUrl": "jdbc:oracle:connection[host]:[port]/service",
"spring.datasource.driver-class-name": "oracle.jdbc.OracleDriver",
"spring.datasource.initialize": "false"
},
"syslog_drain_url": "",
"volume_mounts": [],
"label": "user-provided",
"name": "Database",
"tags": []
}
]
Java Class Java类
ObjectMapper mapper = new ObjectMapper();
//json String to Object
CupsProperties properties = mapper.readValue(VCAP_Services, CupsProperties.class);
System.out.println(properties.getJdbcUrl() + "!!!!!!!!!!!!!!!!!!!");
POJOS POJOS
public class UserProviderWrapper {
@JsonProperty("user-provided")
public List<CupsProperties> cupsProperties;
@JsonProperty("syslog_drain_url")
public String syslog_drain_url;
@JsonProperty("volume_mounts")
public List<String> volume_mounts;
@JsonProperty("label")
public String label;
@JsonProperty("name")
public String name;
@JsonProperty("tags")
public List<String> tags;
//getters and setters
public class CupsProperties {
@JsonProperty("jdbcUrl")
public String jdbcUrl;
@JsonProperty("spring.datasource.driver-class-name")
public String driver;
@JsonProperty("spring.datasource.initialize")
public String initialize;
//getters and setters
Error 错误
Unrecognized field "user-provided" (class rest.springframework.model.CupsProperties), not marked as ignorable (2 known properties: "jdbcUrl", "dataSource"]) at [Source: {"user-provided":[{ "credentials": { "jdbcUrl": "jdbc:oracle:thin:user/pass//host:port/service", "spring.datasource.driver-class-name": "oracle.jdbc.OracleDriver", "spring.datasource.initialize": "false" }, "syslog_drain_url": "", "volume_mounts": [ ], "label": "user-provided", "name": "Oracle", "tags": [ ] }]}; 无法识别的字段“用户提供”(类rest.springframework.model.CupsProperties),未标记为可忽略(2个已知属性:“ jdbcUrl”,“ dataSource”]),位于[来源:{“用户提供”:[{凭据“:{” jdbcUrl“:” jdbc:oracle:thin:user / pass // host:port / service“,” spring.datasource.driver-class-name“:” oracle.jdbc.OracleDriver“,” spring。 datasource.initialize“:” false“},” syslog_drain_url“:”“,” volume_mounts“:[],” label“:”用户提供“,” name“:” Oracle“,” tags:[]}] }; line: 1, column: 19] (through reference chain: rest.springframework.model.CupsProperties["user-provided"])
第1行,第19列](通过参考链:rest.springframework.model.CupsProperties [“用户提供”])
Check below solution and see if it fulfills your need. 检查以下解决方案,看它是否满足您的需求。 You can build on to it if you need to parse more fields.
如果需要解析更多字段,则可以在此基础上构建。
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonParser {
public static void main(String[] args) {
String VCAP_Services = "{\"userProvided\": [{\"credentials\": {\"accessTokenUri\": \"tokenurl\",\"apiUrl\": \"apiurl\",\"clientId\": \"typeofID\",\"clientSecret\": \"secretAf\",\"scope\": \"none\"},\"syslog_drain_url\": \"\",\"volume_mounts\": [],\"label\": \"user-provided\",\"name\": \"OAuth2\",\"tags\": []},{\"credentials\": {\"jdbcUrl\": \"jdbc:oracle:connection[host]:[port]/service\",\"spring.datasource.driver-class-name\": \"oracle.jdbc.OracleDriver\",\"spring.datasource.initialize\": \"false\"},\"syslog_drain_url\": \"\",\"volume_mounts\": [],\"label\": \"user-provided\",\"name\": \"Database\",\"tags\": [] } ] } ";
CupsProperties properties=null;
try {
JSONParser jsonParser = new JSONParser();
JSONObject vcapServiceJSONObject = (JSONObject) jsonParser.parse(VCAP_Services);
for(Object key: vcapServiceJSONObject.keySet()){
String keyStr = (String) key;
JSONArray userProvidedList = (JSONArray) vcapServiceJSONObject.get(keyStr);
Iterator i = userProvidedList.iterator();
while (i.hasNext()) {
JSONObject innerObj = (JSONObject) i.next();
JSONObject credentialsObject = (JSONObject) innerObj.get("credentials");
if(credentialsObject.containsKey("jdbcUrl")){
//set to your pojo objects
System.out.println("JDBC url:" + credentialsObject.get("jdbcUrl"));
}
if(credentialsObject.containsKey("accessTokenUri")){
//set to your pojo objects
System.out.println("Access token URI:" + credentialsObject.get("accessTokenUri"));
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output 输出量
Access token URI:tokenurl
JDBC url:jdbc:oracle:connection[host]:[port]/service
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.