2015-05-10 107 views
0

我使用的PHP數組如下得到一些電話號碼的網絡服務:轉換PHP數組到C#陣列

$to=array("phoneNumbers"=>array('a','b')); 

我需要在C#中使用這個Web服務,但我不知道如何轉換的$對變量C# 我已經嘗試

new string[] {"a","b"} 
new string[] {"phoneNumbers","a","b"} 
new string[][] {"phoneNumbers",new string[]{"a","b"}} 

他們都不是工作

這是我如何調用Web服務PHP

//call soap client 
$soap=new SoapClient("http://www.zamanak.ir/api/soap-v3?wsdl"); 

//get clientId and client Secret from Zamanak.ir 
$soap->clientId=""; 
$soap->clientSecret=""; 

//add your username and password 
$soap->username=""; 
$soap->password=""; 

//get authentication 
$array = $soap->authenticate($soap->clientId,$soap->clientSecret,$soap->username,$soap->password); 
$uid =$array['uid']; 
$token =$array['token']; 
$to=array("phoneNumbers"=>array('09121232131','091212313221','091254545545')); 
$r=$soap->calculateCost($soap->clientId, $soap->clientSecret, $uid, $token,'iran',$to, '3880', $repeatTotal = 1); 
var_export($r); 
die; 
+0

什麼樣的web服務的是它,你怎麼稱呼它的那一刻? – ZoolWay

+0

爲什麼你不覺得JSON? –

+0

您正在使用的數組目前多於一個深度級別? 如果是這樣,字典解決方案將不會有任何好處;-) –

回答

1

我會說這是裝箱的關聯數組,所以在C#中應該是這樣的:

Dictionary<String, String[]> array = new Dictionary<String, String[]> 
{ 
    { "phoneNumbers", new String[]{"a","b"} } 
}; 

,那麼你訪問它像

array["phoneNumbers"][0] //returns "a" 

如果您需要使用它在Web服務中需要一個可序列化的通用字典,如提到的here

所以我的示例web服務看起來是這樣的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 
using System.Xml.Serialization; 

namespace WcfService1 
{ 
    [XmlRoot("dictionary")] 
    public class SerializableDictionary<TKey, TValue> 
     : Dictionary<TKey, TValue>, IXmlSerializable 
    { 
     #region IXmlSerializable Members 
     public System.Xml.Schema.XmlSchema GetSchema() 
     { 
      return null; 
     } 

     public void ReadXml(System.Xml.XmlReader reader) 
     { 
      XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); 
      XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); 

      bool wasEmpty = reader.IsEmptyElement; 
      reader.Read(); 

      if (wasEmpty) 
       return; 

      while (reader.NodeType != System.Xml.XmlNodeType.EndElement) 
      { 
       reader.ReadStartElement("item"); 

       reader.ReadStartElement("key"); 
       TKey key = (TKey)keySerializer.Deserialize(reader); 
       reader.ReadEndElement(); 

       reader.ReadStartElement("value"); 
       TValue value = (TValue)valueSerializer.Deserialize(reader); 
       reader.ReadEndElement(); 

       this.Add(key, value); 

       reader.ReadEndElement(); 
       reader.MoveToContent(); 
      } 
      reader.ReadEndElement(); 
     } 

     public void WriteXml(System.Xml.XmlWriter writer) 
     { 
      XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); 
      XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); 

      foreach (TKey key in this.Keys) 
      { 
       writer.WriteStartElement("item"); 

       writer.WriteStartElement("key"); 
       keySerializer.Serialize(writer, key); 
       writer.WriteEndElement(); 

       writer.WriteStartElement("value"); 
       TValue value = this[key]; 
       valueSerializer.Serialize(writer, value); 
       writer.WriteEndElement(); 

       writer.WriteEndElement(); 
      } 
     } 
     #endregion 
    } 

    [ServiceContract] 
    public interface IService1 
    { 
     [OperationContract] 
     string calculateCost(SerializableDictionary<String, String[]> to);   
    } 

    public class Service1 : IService1 
    { 
     public string calculateCost(SerializableDictionary<String, String[]> to) 
     { 
      StringBuilder output = new StringBuilder(); 
      foreach (KeyValuePair<String, String[]> item in to) 
      { 
       output.AppendLine(string.Format("{0} => {1}", item.Key, String.Join(",", item.Value))); 
      } 
      return output.ToString(); 
     }  
    } 
} 
+0

我試試這個,但它沒有工作!我得到這個錯誤:「生成XML文檔時出錯。」 – Kahbazi

+0

如果您需要將通用詞典序列化爲xml,請看http://weblogs.asp.net/pwelter34/444961。但這是另一個話題..你問了如何在c#中表示這個php結構,我回答了這個問題。 –

+0

謝謝。你能告訴我如何使用xml生成這個數組嗎?它可能解決我的問題。 – Kahbazi