Java 返回 JSONArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17757886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:53:14  来源:igfitidea点击:

Java return JSONArray

javajson

提问by Fonzy

I'm trying to create a jsonarray from a Map in java. I'm passing it in to a javascript variable. But i don't know why the mac and status are blank, any help much appreciated.

我正在尝试从 Java 中的 Map 创建一个 jsonarray。我将它传递给一个 javascript 变量。但我不知道为什么 Mac 和状态为空白,非常感谢任何帮助。

what i need:

我需要的:

[{"12345":{"mac":"FFFFFFFF", "status":"ON"}]

What i am getting with my current code:

我现在的代码得到了什么:

[{"12345":{}]

Here is my code,

这是我的代码,

public class Details {

public JSONArray getResult() {
    return JSONArray.fromObject(this.det);
}
public Map det = new HashMap();

public results() {
   ResultSet rs;
   det.put(rs.getString(1), new NodeDetails(rs.getString(2), rs.getString(3));
}
class NodeDetails {
    public final String MAC;
    public final String status;

    public NodeDetails(final String ma,final String st) {
        this.MAC = ma;
        this.status = st;
    }
  }
}

回答by user2507946

Do you have any limitation on any library? I mean are you using JSON library from http://org.jsonor which library?

你对任何图书馆有任何限制吗?我的意思是你使用的是来自http://org.json 的JSON 库还是哪个库?

Following is the code that I've tried using JSON library from http://org.json:

以下是我尝试使用来自http://org.json 的JSON 库的代码:

public class Test {

    public static class NodeDetails {
        public final String MAC;
        public final String status;

        public NodeDetails(final String ma, final String st) {
            this.MAC = ma;
            this.status = st;
        }
    }

    public static void main(String[] args) throws Exception {
        Map<String, NodeDetails> map = new HashMap<String, NodeDetails>();
        // do something with you ResultSet? and populate the map ;)
        map.put("12345", new NodeDetails("FFFFFF", "ON"));

        JSONObject jsonMap = new JSONObject();
        for (Map.Entry<String, NodeDetails> entry : map.entrySet()) {
            JSONObject object = new JSONObject();
            object.put(entry.getValue().MAC, entry.getValue().status);
            jsonMap.put(entry.getKey(), object);
        }

        JSONArray jsonArray = new JSONArray();
        jsonArray.put(jsonMap);

        System.out.println(jsonArray.toString());

    }
}

You can read more about the API here: http://json.org/java/

您可以在此处阅读有关 API 的更多信息:http: //json.org/java/

回答by Ankur Lathi

JsonArray.fromObject-- Creates a JSONArray. Inspects the object type to call the correct JSONArray factory method. Accepts JSON formatted strings, arrays and Collections.

JsonArray.fromObject-- 创建一个 JSONArray。检查对象类型以调用正确的 JSONArray 工厂方法。接受 JSON 格式的字符串、数组和集合

And Map which you are passing is not JSON formatted. So try using add() method on JsonArray Or put() method on JsonObject.

您传递的 Map 不是 JSON 格式的。所以尝试在 JsonArray 上使用 add() 方法或在 JsonObject 上使用 put() 方法。