英文:
How to fetch a parameter value from parsed String (Json Request)
问题
// Java代码示例
String json = "..."; // 将长json请求作为字符串保存在这里
// 使用 JSON 解析库解析 JSON 字符串,获取 "siteName" 参数值
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
String siteName = jsonObject.get("siteName").getAsString();
// 输出 "siteName" 参数值
System.out.println("Site Name: " + siteName);
请注意,上述代码示例中的 json
变量应该被替换为您实际的 JSON 请求字符串。在您的项目中,您需要使用适当的 JSON 解析库,如 Gson 或 Jackson,以便进行 JSON 解析操作。
英文:
I have json request which I have parsed as a String and I need to fetch a param value of "sitename" that is "SIT-SITE-2219" from the long json request using Java.Please help .
<!-- language: lang-html -->
"siteCounty":"UC",
"alocCounty":"test2",
"alocLat":"39.98",
"siteName":"SIT-SITE-2219",
"requestedBy":"UnitTester",
"msc":"test",
<!-- end snippet -->
答案1
得分: 1
Use gson
YourClass yourClass = new Gson().fromJson("{\"siteCounty\":\"UC\",
\"alocCounty\":\"test2\",
\"alocLat\":\"39.98\",
\"siteName\":\"SIT-SITE-2219\",
\"requestedBy\":\"UnitTester\",
\"msc\":\"test\"}", YourClass.class);
yourClass.getSiteName();
英文:
Use gson
YourClass yourClass = new Gson().fromJson("siteCounty":"UC",
"alocCounty":"test2",
"alocLat":"39.98",
"siteName":"SIT-SITE-2219",
"requestedBy":"UnitTester",
"msc":"test", YourClass.class);
yourClass.getSiteName();
答案2
得分: 1
你可以使用无类型映射(untyped map)来读取 JSON,然后进行键值(kv)查找。类似以下的代码示例:
String jsonString = "{\"siteCounty\":\"UC\",\n" +
" \"alocCounty\":\"test2\",\n" +
" \"alocLat\":\"39.98\",\n" +
" \"siteName\":\"SIT-SITE-2219\",\n" +
" \"requestedBy\":\"UnitTester\",\n" +
" \"msc\":\"test\"}";
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, String>> typeRef
= new TypeReference<HashMap<String, String>>() {};
Map<String, String> map = null;
try {
map = mapper.readValue(jsonString, typeRef);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(map.get("siteName"));
此示例在 Java 8 上使用了以下依赖:
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.11.0'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.4'
英文:
You can use untyped map to read the json, and then do a kv lookup. Something like the following:
String jsonString = "{\"siteCounty\":\"UC\",\n" +
" \"alocCounty\":\"test2\",\n" +
" \"alocLat\":\"39.98\",\n" +
" \"siteName\":\"SIT-SITE-2219\",\n" +
" \"requestedBy\":\"UnitTester\",\n" +
" \"msc\":\"test\"}";
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, String>> typeRef
= new TypeReference<HashMap<String, String>>() {};
Map<String, String> map = null;
try {
map = mapper.readValue(jsonString, typeRef);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(map.get("siteName"));
Example uses the following dependencies on Java:8
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.11.0'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.4'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论