首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的JSON数组中只有最后一个json元素?

为什么我的JSON数组中只有最后一个json元素?
EN

Stack Overflow用户
提问于 2013-06-11 17:17:59
回答 1查看 270关注 0票数 0

为什么我的JSON数组中只有最后一个json元素?

这是我的代码:

代码语言:javascript
运行
复制
@GET
@Path("inregistrareHarta")
@Produces(MediaType.APPLICATION_JSON)
public void getListPuncte(@QueryParam("idTransporturi") int idTransporturi) 
{
  PuncteDAO puncteInformation = new PuncteDAO();
  ArrayList<Puncte> puncteList = puncteInformation.getPuncte(idTransporturi);

  Gson gson = new Gson();
  JsonArray arrayObj=new JsonArray();
  for(int i=0;i<puncteList.size();i++)
  {
    Puncte puncte = puncteList.get(i);
    JsonElement puncteObj = gson.toJsonTree(puncte);   
    arrayObj.add(puncteObj);
  }
  JsonObject myObj = new JsonObject();
  myObj.add("puncteList", arrayObj);
  System.out.println(myObj.toString());
}

这是我得到的结果:

代码语言:javascript
运行
复制
{"puncteList":
  [
    {"idPuncte":7,"idTransport":2,"latitudine":190.0,"longitudine":150.0,"descriere":"2_4"},
    {"idPuncte":7,"idTransport":2,"latitudine":190.0,"longitudine":150.0,"descriere":"2_4"},
    {"idPuncte":7,"idTransport":2,"latitudine":190.0,"longitudine":150.0,"descriere":"2_4"},
    {"idPuncte":7,"idTransport":2,"latitudine":190.0,"longitudine":150.0,"descriere":"2_4"}
  ]
}

这就是我所期待的结果:

代码语言:javascript
运行
复制
{"puncteList":
  [ 
    {"idPuncte":4,"idTransport":2,"latitudine":19.0,"longitudine":0.0,"descriere": "‌2_1"}, 
    {"idPuncte":5,"idTransport":2,"latitudine":10.0,"longitudine": 50.0,"descriere":"‌2_2"}, 
    {"idPuncte":6,"idTransport":2,"latitudine":1.0,"longitudine":1.0,"descriere":"2_‌3"},
    {"idPuncte":7,"idTransport":2,"latitudine":30.0,"longitudine":50.0,"descriere":"‌2_4"}
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2013-06-11 22:56:03

在您期望的输出中,结束]可能是一个打字错误。

您期望的输出是简单的,而不是有效的JSON表达式。你可以拥有这个:

代码语言:javascript
运行
复制
[
  "puncteList", 
  {"idPuncte":4,"idTransport":2,"latitudine":19.0,"longitudine":0.0,"descriere": "‌2_1"}, 
  {"idPuncte":5,"idTransport":2,"latitudine":10.0,"longitudine": 50.0,"descriere":"‌2_2"}, 
  {"idPuncte":6,"idTransport":2,"latitudine":1.0,"longitudine":1.0,"descriere":"2_‌3"},
  {"idPuncte":7,"idTransport":2,"latitudine":30.0,"longitudine":50.0,"descriere":"‌2_4"}
]

但是,如果"puncteList“是一个键,它必须有一个值:标量、数组或另一个对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17040528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档