【问题标题】:Process a POST recieved from server in JSON format [closed]以 JSON 格式处理从服务器收到的 POST [关闭]
【发布时间】:2013-03-12 16:01:40
【问题描述】:

我将如何接收一个要发布到服务器的 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
    }]}

【问题讨论】:

  • 您使用的是什么服务器技术?小服务程序?
  • 是的,servlet 与 java 类结合使用..

标签: java javascript html json


【解决方案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 */
}

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

【讨论】:

    【解决方案2】:
    $(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中发送数据。

    【讨论】:

    • 你将如何从会话中取回数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多