我将如何接收发布到服务器的JSON文件,并将其处理得足够远,可以将其输入MySQL数据库
应该发送到服务器的JSON示例如下,
{"messages":[{
"messageId": "9ebd1279-d232-49a6-b074-b43b48b8332c",
"userFromId": 1,
"scope": 0,
"messageContent": "hello 123\r\n",
"messageSentDate": 1363090081570,
"messageRecievedDate": 0,
"messageReadDate": 0,
"messageLastModified": 0,
"readReceiptRequired": false
},{
"messageId": "c7b6005a-e493-4074-86bd-6b6462ea3f48",
"userFromId": 1,
"scope": 1,
"messageContent": "hello ",
"messageSentDate": 1363084238352,
"messageRecievedDate": 0,
"messageReadDate": 0,
"messageLastModified": 0,
"readReceiptRequired": false
}]}
答案 0 :(得分:1)
发布时:
/* i would use a hidden field for maintaing count, say totalmessages */
/* use a loop for appending the name with a value (message0, message1, message2) */
for example when generating html from php..
for($i=0; i<$messagecount;$ i++)
{
echo "<input type=text name=messageid$i>"; /*in case you are taking messages from input box*/
}
在服务器中,反过来。
/*Read the total messages first. $count = $_POST["totalmessages"];*/
//Then use a loop
for($i=0; $i < $count; $i++)
{
/*Read the posted value using $i*/
$messageid=$_POST["messageId$i"];
.......
/*Write it into database */
}
希望这接近你所寻找的
答案 1 :(得分:0)
$(function(){
$.ajax(
{
data: mydata, // mydata is your json
method:POST,
url: ../MyServlet,
success: function(response){alert(response);
}
});
在MyServlet中
public doPost(HTTPServletRequest req, HTTPServletResponse res)
{
JSONObject jObj = new JSONObject(request.getParameter("mydata")); // this parses the json
Iterator it = jObj.keys(); //gets all the keys
while(it.hasNext())
{
String key = it.next(); // get key
Object o = jObj.get(key); // get value
session.putValue(key, o); // store in session
}
}
现在以mysql发送数据。