0

なぜ私の JSON 配列は最後の json 要素なのですか?

これは私のコードです:

@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());
}

これは私が得ている結果です:

{"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"}
  ]
}

そして、これは私が期待している結果です:

{"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"}
  ]
}
4

1 に答える 1

0

予想される出力では、終了 ] はおそらくタイプミスです。

期待される出力は単純で、有効な JSON 式ではありません。あなたはこれを持つことができます:

[
  "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」がキーの場合は、値 (スカラー、配列、または別のオブジェクト) が必要です。

于 2013-06-11T14:56:02.593 に答える