Php 如何将关联数组转换为JSON,并使用ASP.NET将其作为POST请求的一部分发送?

Php 如何将关联数组转换为JSON,并使用ASP.NET将其作为POST请求的一部分发送?,php,asp.net,json,post,Php,Asp.net,Json,Post,我正在为一个用PHP开发的RESTAPI编写一些示例代码片段,虽然我可以设法得到一个“Ruby”示例,但我还没有找到一个像样的ASP.NET(figures)示例。我想知道是否有ASPer可以帮助完成以下PHP的基本翻译,该PHP发出一个POST请求,并将JSON字符串作为其负载 需要注意的主要问题是POST需要一个命名参数“data”,它是一个JSON字符串 $key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // This woul

我正在为一个用PHP开发的RESTAPI编写一些示例代码片段,虽然我可以设法得到一个“Ruby”示例,但我还没有找到一个像样的ASP.NET(figures)示例。我想知道是否有ASPer可以帮助完成以下PHP的基本翻译,该PHP发出一个POST请求,并将JSON字符串作为其负载

需要注意的主要问题是POST需要一个命名参数“data”,它是一个JSON字符串

    $key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';    // This would be your customer key
    $map='USA';
    $accountid='000';                                   // this would be your account id
    // add some zips to an array
    $zips[]=22201;
    $zips[]=90210;
    $zips[]=10001;

    // We encode an array into JSON
    $data = array("map" => $map, "zips"=>$zips, "accountid" => $accountid, "custkey" => $key);                                                                    
    $data_string = json_encode($data);                       
    // IMPORTANT - the API takes only one POST parameter - data 
    $postdata="data=$data_string";

    // we use curl here, but Zend has Rest interfaces as well...
    $ch = curl_init('https://www.example.com//test/');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_POST, 1);         // make sure we submit a POST!

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
      $result=curl_error($ch);
    } else {
      curl_close($ch);
    }

    $jsonObj=json_decode($result);
    if($jsonObj->success){
      $coordinates=$jsonObj->coordinates;               
      foreach($coordinates->coordinates as $coord){
        //... application logic here ( construct XML for the map )
      }
    }
谢谢你的帮助-我不喜欢这样的东西,但也许它会帮助别人在未来以及

R

作为对命令的回应——我真正的帮助请求源于缺乏ASP环境来调试/测试示例。例如-@Chris发布了一个链接,虽然从表面上翻译它看起来很简单(尝试如下),但我的问题是我不仅仅是以正常的发布方式发送数据:

param=val&param2=val2 
它需要像:

data={JSONString}
其中JSONString由关联数组生成。然后,ASP中的关联数组(或者说显然缺少关联数组?--)通常会出现问题,然后如何将不存在的关联数组编码为JSON字符串,或者如果我尝试使用NamedValueCollection来代替?ASP中对JSON的支持似乎也是参差不齐的,所以我肯定需要一个特殊的接口

using System.Web;

Uri address = new Uri("http://www.example.com/test/");
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
string map = "USA";
string accountid = "000";
string data =""; 
NameValueCollection data = new NameValueCollection();
data.Add("custkey", key);
data.Add("map", map);
data.Add("accountid", accountid);
由于ASP实际上没有关联数组,如何将数据转换为JSON

string jsondata="";



StringBuilder data = new StringBuilder();
当使用UrlEncoding时,下面的行是否会中断内容

data.Append("data=" + HttpUtility.UrlEncode(jsondata));

// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

// Set the content length in the request headers
request.ContentLength = byteData.Length;

// Write data
using (Stream postStream = request.GetRequestStream())
{
  postStream.Write(byteData, 0, byteData.Length);
}
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
   // Get the response stream
   StreamReader reader = new StreamReader(response.GetResponseStream());
   // Console application output
   string jsonResponse =reader.ReadToEnd());
}
所以我想我的问题应该给出上面的黑客例子-有人能告诉我这是正确的还是有帮助的

你试过jSon.Net吗?例如:

产品产品=新产品();
product.Name=“苹果”;
产品有效期=新的日期时间(2008年12月28日);
产品价格=399万元;
product.size=新字符串[]{“小”、“中”、“大”};
字符串json=JsonConvert.SerializeObject(产品);
//{
//“名称”:“苹果”,
//“到期日”:“2008-12-28:00:00”,
//“价格”:3.99,
//“尺寸”:[
//“小”,
//“中等”,
//“大”
//  ]
//}
Product deserializedProduct=JsonConvert.DeserializeObject(json);

结合我发现这是最好的.Net/JSon组合。

-5秒在Google中。@ChrisL我认为OP发布这篇文章的方式很有礼貌(尽管缺乏努力)。没必要粗鲁。嗨,罗斯!对于堆栈溢出的范围,代码转换请求实际上不能很好地工作。如果你自己也尝试过翻译,并且有一个关于你被困在哪里的具体问题,那将是一个合适的问题。现在,这太宽泛了,可能对未来的访客没有帮助,并且显示出一种缺乏努力的表现(注意,我不是说你没有尝试过任何东西,我只是说这就是它看起来的样子)。谢谢@jadarnel27-我理解-这不是说我没有尝试过什么,而是说我不能真正“尝试”由于缺少ASP环境而导致的任何问题。我遇到了Chris提供的许多例子,但问题围绕ASP中的关联数组咆哮而出现,然后从那里开始走下坡路,我认为ASP兽医可以快速推出一些东西来帮助我解决问题。我们将看看上面的阐述是否不仅仅是反对票。
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "Expiry": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);