-1

我将如何去接收一个 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
    }]}
4

2 回答 2

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 */
}

希望这与您正在寻找的内容接近

于 2013-03-12T16:37:54.170 回答
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中发送数据。

于 2013-03-12T16:07:09.983 回答