java ajax返回数组,Java返回JSONArray

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.

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;

}

}

}

解决方案

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

Following is the code that I've tried using JSON library from http://org.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 map = new HashMap();

// do something with you ResultSet? and populate the map ;)

map.put("12345", new NodeDetails("FFFFFF", "ON"));

JSONObject jsonMap = new JSONObject();

for (Map.Entry 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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值