当前位置: 动力学知识库 > 问答 > 编程问答 >

Convert PHP Array To C# Array

问题描述:

I'm using a web service that gets some phone numbers in php array as below :

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

I need to Use this web service in C# but I don't know how to Convert the $to variable to C#

I already try

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

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

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

None of them is working

This is how I call the web service in 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;

网友答案:

I would say it is crating an associative array, so in C# it should be something like this:

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

then you access it like

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

If you need to use it in a web service you will need a serializable generic dictionary like the one mentioned here.

so my example web service would look something like:

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();
        }      
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: