簡體   English   中英

在Web Service AMX中讀取JSON

[英]Read json in web service asmx

我在C#中創建Web服務以接收json並將其插入數據庫:

public string SaveData(string resultData)
{

DLR service = new DLR();

string filePath = Server.MapPath("~/Error.txt");

using (StreamWriter writer = new StreamWriter(filePath, true))
{
    writer.WriteLine("Json is sent :" + resultData +
       "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
    writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
}

RootObject obj = JsonConvert.DeserializeObject<RootObject>(resultData,settings);
    foreach (JsonData item in obj.results)
    {
        {
            var connStr = ConfigurationManager.ConnectionStrings["myCon1"].ConnectionString;
            SqlConnection con = new SqlConnection(connStr);
            try
            {
                SqlCommand cmd = new SqlCommand("[insert_RDL]", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@msgId", item.msgId);
                cmd.Parameters.AddWithValue("@to_mobile", item.to);
                cmd.Parameters.AddWithValue("@status", item.status);
                cmd.Parameters.AddWithValue("@ttest", "");
                cmd.Parameters.AddWithValue("@newtest", "");
                con.Open();
                cmd.ExecuteNonQuery();
            }
            finally
            {
                //                    
            }
        }
    }
    return "OK ";
}

當我在apxx頁面中通過ajax發布json時,頁面正在發送json而沒有問題。 我在error.txt中看到json。

當我通過php發布json時:

<?php
$data = '{"results": [{"msgId": "4000","to": "9665312114","status": "D"}, 
{"msgId": "859911880","to": "966535112578","status": "N"}, 
{"msgId":"859911880","to": "966535112579","status": "S"}]}' ;
$phpObj =  json_decode($data);
print_r($phpObj);
$resultData =  json_encode($phpObj);
print_r($resultData);
$client = new SoapClient("http://asdm.sa/DLR.asmx?wsdl");
$response = $client->SaveData($data);
print_r($response);
?>

----------

json正在發送為NULL。 為什么頁面不發送json。

當我打開error.txt時:

向Json發送了:{“結果”:[{“ msgId”:“ 001”,“至”:“ 9665312114”,“狀態”:“ D”},{“ msgId”:“ 859911880”,“至”:“ 966535112578“,”狀態“:” N“},{” msgId“:” 859911880“,”收件人“:” 966535112579“,”狀態“:” S“}]}日期:2018/2/3 6:59:下午2點


Json已發送:

日期:2/3/2018 7:31:35 PM

看起來問題出在發送的參數中。 請嘗試下一個代碼

<?php
$data = '{"results": [{"msgId": "4000","to": "9665312114","status": "D"}, 
{"msgId": "859911880","to": "966535112578","status": "N"}, 
{"msgId":"859911880","to": "966535112579","status": "S"}]}' ;
$phpObj =  json_decode($data);
print_r($phpObj);
$resultData =  json_encode($phpObj);
print_r($resultData);
$client = new SoapClient("http://asdm.sa/DLR.asmx?wsdl");
$param = array('resultData' => $data);
$response = $client->SaveData($param);
print_r($response);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM