如何从解析后的字符串(JSON 请求)中获取参数值

huangapple go评论97阅读模式
英文:

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 -->

&quot;siteCounty&quot;:&quot;UC&quot;,
         &quot;alocCounty&quot;:&quot;test2&quot;,
         &quot;alocLat&quot;:&quot;39.98&quot;,
         &quot;siteName&quot;:&quot;SIT-SITE-2219&quot;,
         &quot;requestedBy&quot;:&quot;UnitTester&quot;,
         &quot;msc&quot;:&quot;test&quot;,

<!-- 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 = &quot;{\&quot;siteCounty\&quot;:\&quot;UC\&quot;,\n&quot; +
                &quot;         \&quot;alocCounty\&quot;:\&quot;test2\&quot;,\n&quot; +
                &quot;         \&quot;alocLat\&quot;:\&quot;39.98\&quot;,\n&quot; +
                &quot;         \&quot;siteName\&quot;:\&quot;SIT-SITE-2219\&quot;,\n&quot; +
                &quot;         \&quot;requestedBy\&quot;:\&quot;UnitTester\&quot;,\n&quot; +
                &quot;         \&quot;msc\&quot;:\&quot;test\&quot;}&quot;;

        ObjectMapper mapper = new ObjectMapper();

        TypeReference&lt;HashMap&lt;String, String&gt;&gt; typeRef
                = new TypeReference&lt;HashMap&lt;String, String&gt;&gt;() {};
        Map&lt;String, String&gt; map = null;
        try {
           map =  mapper.readValue(jsonString, typeRef);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(map.get(&quot;siteName&quot;));

Example uses the following dependencies on Java:8

   compile group: &#39;com.fasterxml.jackson.core&#39;, name: &#39;jackson-annotations&#39;, version: &#39;2.11.0&#39;
   compile group: &#39;com.fasterxml.jackson.core&#39;, name: &#39;jackson-databind&#39;, version: &#39;2.9.4&#39;

huangapple
  • 本文由 发表于 2020年5月29日 11:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/62078240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定