无法获取 JSON 对象的元素

发布于 2025-01-18 06:13:56 字数 1492 浏览 2 评论 0 原文

我有一个包含一些json对象的jsonobject

  "paths": {
    "/api/{version}/RAW/getrawdata": {
      "get": {
        "tags": [
          "RAw"
        ],
        "summary": "/getrawdata",
        "parameters": [
          {
            "name": "token",
            "in": "header",
            "description": "gettoken",
            "schema": {
              "type": "string"
            },
    "/api/{version}/filtered/getfinaldatadata": {
    "get": {
        "tags": [
          "filtered"
        ],
        "summary": "/getfinaldatadata",
Now I want to get the elements summary and tags and want to return the values. Its easy to get the value of summary but tags has [] so I don't know how to take the value e.g RAW . I was trying something like this

package beans;

import org.apache.camel.Exchange;
import org.apache.camel.Header;
public class URIpattern {
    @SuppressWarnings("deprecation")
    public String URI(JSONObject json,
            @Header (Exchange.SLIP_ENDPOINT) String previous) {
        if(previous!=null){
             return null;


  JSONObject paths= json.getJSONObject("paths");
  JSONObject summary = operation.getString("summary");
    JSONObject tags = operation.getJSONObject("tags");
      return tags + summary;

但这没有做任何事情。有人可以指导我,因为我是Java的新手。 任何帮助将不胜感激。提前致谢

I have a jsonobject that contains some json objects

  "paths": {
    "/api/{version}/RAW/getrawdata": {
      "get": {
        "tags": [
          "RAw"
        ],
        "summary": "/getrawdata",
        "parameters": [
          {
            "name": "token",
            "in": "header",
            "description": "gettoken",
            "schema": {
              "type": "string"
            },
    "/api/{version}/filtered/getfinaldatadata": {
    "get": {
        "tags": [
          "filtered"
        ],
        "summary": "/getfinaldatadata",

Now I want to get the elements summary and tags and want to return the values. Its easy to get the value of summary but tags has [] so I don't know how to take the value e.g RAW .
I was trying something like this

package beans;

import org.apache.camel.Exchange;
import org.apache.camel.Header;
public class URIpattern {
    @SuppressWarnings("deprecation")
    public String URI(JSONObject json,
            @Header (Exchange.SLIP_ENDPOINT) String previous) {
        if(previous!=null){
             return null;


  JSONObject paths= json.getJSONObject("paths");
  JSONObject summary = operation.getString("summary");
    JSONObject tags = operation.getJSONObject("tags");
      return tags + summary;

but it is not doing any thing. Can someone please guide me since I am new to java.
Any help would be appreciated. Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

來不及說愛妳 2025-01-25 06:13:56

好吧,我猜你正在使用这个 Object 并且在定义的类中您有函数 getJSONArray 之后只需使用新的 ArrayObject 并从中提取您需要的数据。

Well I guess you are using this Object and inside the defined class you have the function getJSONArray after that just use the new ArrayObject and pull the data what you need out of it.

浪漫之都 2025-01-25 06:13:56

在 JSON 中,[] 中的值被视为数组。因此,如果您需要提取 tags,则需要将它们放入一个数组中,例如,

 String json = "{\"tags\":[\"1\",\"2\",\"3\"]}";
 JSONObject object = new JSONObject(json);
 JSONArray array = object.getJSONArray("tags");
 System.out.println(array);

PS 我找不到有问题的 json 是有效的,因此我使用了一些 exmaple 来解释JSONArray。

In JSON values are present in [] are treated as arrays. So if you need to extract the tags, you will need to get them in a array like,

 String json = "{\"tags\":[\"1\",\"2\",\"3\"]}";
 JSONObject object = new JSONObject(json);
 JSONArray array = object.getJSONArray("tags");
 System.out.println(array);

P.S. I could not find the json posted in question to be valid, hence I have used some exmaple to explain the JSONArray.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文