2013-03-12 33 views
-1

我怎麼會去recieveing一個JSON文件被髮送到服務器並處理它遠遠不夠它可以輸入到一個MySQL數據庫處理一個POST從服務器JSON格式

的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 
    }]} 
+1

什麼服務器技術? Servlet的? – 2013-03-12 16:04:39

+0

是的,與Java類結合的servlet .. – user2137541 2013-03-12 16:39:45

回答

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中的數據。

+0

然後,您將如何從會話中取回數據? – user2137541 2013-03-12 16:45:56

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

希望這是接近你是,你使用尋找