我有一个从数据库中提取的自定义对象的一般列表:
List<MyEntity> myEntities = Arrays.asList(/** MyEntity[] **/);
我尝试将其转换为JSON并发送到Node正在侦听的服务器,但失败。
转换为JSON的列表如下:
[
{
"_id": 0,
"_myEntityAccessPermission": 0,
"_myEntityContainerGUID": -1,
"_myEntityGUID": 119,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1,
"_myEntitySubType": "my_user_entity",
"_myEntityType": "my_user_entity",
"_timeCreated": "Thu Aug 16 08:56:02 2018\n",
"_timeUpdated": "Thu Aug 16 08:56:02 2018\n",
"myUserEntity": {
"_email": "TryMyilo@Yahoo.com",
"_fullNames": "Oliver ",
"_sessionCode": 0,
"_id": 0,
"_myEntityAccessPermission": -1,
"_myEntityContainerGUID": -1,
"_myEntityGUID": -1,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1
}
},
{
"_id": 0,
"_myEntityAccessPermission": 0,
"_myEntityContainerGUID": -1,
"_myEntityGUID": 120,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1,
"_myEntitySubType": "my_user_entity",
"_myEntityType": "my_user_entity",
"_timeCreated": "Thu Aug 16 11:44:24 2018\n",
"_timeUpdated": "Thu Aug 16 11:44:24 2018\n",
"myUserEntity": {
"_email": "Eddy",
"_fullNames": "Eddy Koma Myilo Oliver",
"_sessionCode": 0,
"_id": 0,
"_myEntityAccessPermission": -1,
"_myEntityContainerGUID": -1,
"_myEntityGUID": -1,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1
}
},
{
"_id": 0,
"_myEntityAccessPermission": 0,
"_myEntityContainerGUID": -1,
"_myEntityGUID": 121,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1,
"_myEntitySubType": "my_user_entity",
"_myEntityType": "my_user_entity",
"_timeCreated": "Thu Aug 16 11:45:18 2018\n",
"_timeUpdated": "Thu Aug 16 11:45:18 2018\n",
"myUserEntity": {
"_email": "Kim",
"_fullNames": "Kim",
"_sessionCode": 0,
"_id": 0,
"_myEntityAccessPermission": -1,
"_myEntityContainerGUID": -1,
"_myEntityGUID": -1,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1
}
},
{
"_id": 0,
"_myEntityAccessPermission": 0,
"_myEntityContainerGUID": -1,
"_myEntityGUID": 122,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1,
"_myEntitySubType": "my_user_entity",
"_myEntityType": "my_user_entity",
"_timeCreated": "Thu Aug 16 15:40:05 2018\n",
"_timeUpdated": "Thu Aug 16 15:40:05 2018\n",
"myUserEntity": {
"_email": "Fam",
"_fullNames": "Eddy Oliver Maurice Esther Ann George Paul ",
"_sessionCode": 0,
"_id": 0,
"_myEntityAccessPermission": -1,
"_myEntityContainerGUID": -1,
"_myEntityGUID": -1,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1
}
},
{
"_id": 0,
"_myEntityAccessPermission": 0,
"_myEntityContainerGUID": -1,
"_myEntityGUID": 123,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1,
"_myEntitySubType": "my_user_entity",
"_myEntityType": "my_user_entity",
"_timeCreated": "Fri Aug 17 02:20:25 2018\n",
"_timeUpdated": "Fri Aug 17 02:20:25 2018\n",
"myUserEntity": {
"_email": "Dan",
"_fullNames": "Dan ",
"_sessionCode": 0,
"_id": 0,
"_myEntityAccessPermission": -1,
"_myEntityContainerGUID": -1,
"_myEntityGUID": -1,
"_myEntityOwnerGUID": -1,
"_myEntitySiteGUID": -1
}
}
]
这是我要努力工作的方法
private void sendPost() {
String postURL = "http://192.168.100.11:3000/sound";
RequestQueue queue = Volley.newRequestQueue(mContext);
JSONArray array=new JSONArray();
List<MyEntity> myEntities = Arrays.asList(new MyPersLibRead().myPersGetALLMyEntityUnSyched());
for (MyEntity myEntity : myEntities) {
Gson gson = new Gson();
String json = gson.toJson(myEntity);
try {
array.put(new JSONObject(json));
} catch (JSONException e) {
e.printStackTrace();
}
Log.v("MyApp", json);
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("filter",array);
} catch (JSONException e) {
e.printStackTrace();
}
Log.v("MyApp",">>>>>>>>> " + array.toString());
JsonObjectRequest jobReq = new JsonObjectRequest(Request.Method.POST, postURL, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Log.v("MyApp", jsonObject.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.v("MyApp", "ERR >>>");
}
});
queue.add(jobReq);
}
在节点端:
// This responds a POST request
app.post('/sound', function (req, res) {
console.log("Got a POST request");
console.log(req);
res.send(req);
});
节点引发的错误:
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at stringify (/var/www/html/node/my_node_pers_remote/node_modules/express/lib/response.js:1119:12)
at ServerResponse.json (/var/www/html/node/my_node_pers_remote/node_modules/express/lib/response.js:260:14)
at ServerResponse.send (/var/www/html/node/my_node_pers_remote/node_modules/express/lib/response.js:158:21)
at /var/www/html/node/my_node_pers_remote/app.js:39:9
at Layer.handle [as handle_request] (/var/www/html/node/my_node_pers_remote/node_modules/express/lib/router/layer.js:95:5)
at next (/var/www/html/node/my_node_pers_remote/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/www/html/node/my_node_pers_remote/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/html/node/my_node_pers_remote/node_modules/express/lib/router/layer.js:95:5)
at /var/www/html/node/my_node_pers_remote/node_modules/express/lib/router/index.js:281:22
如何通过JSON将数据成功发送到Node? p>
答案 0 :(得分:0)
只需使用Gson。这将帮助您将列表隐藏为Json字符串
Gson gson = new Gson();
// convert your list to json
String jsonCartList = gson.toJson(cartList);
// print your generated json
System.out.println("jsonCartList: " + jsonCartList);