在PHP中输入Json时出错

在PHP中输入Json时出错,php,json,parsing,Php,Json,Parsing,因此,我尝试对来自Web服务的XML响应进行编码,如下所示: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

因此,我尝试对来自Web服务的XML响应进行编码,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAllAlbumsResponse xmlns="http://tempuri.org/">
      <GetAllAlbumsResult>
        <Album>
          <IdAlbum>int</IdAlbum>
          <Name>string</Name>
          <Artist>string</Artist>
          <NumberTracks>int</NumberTracks>
          <Price>double</Price>
          <Stock>int</Stock>
          <ImagePath>string</ImagePath>
          <AlbumType>string</AlbumType>
        </Album>
      </GetAllAlbumsResult>
    </GetAllAlbumsResponse>
  </soap:Body>
</soap:Envelope>
{"any":"1<\/IdAlbum>Wrath<\/Name>Lamb Of God<\/Artist>11<\/NumberTracks>7.29<\/Price>200<\/Stock>http:\/\/www.nuclearblast.de\/static\/articles\/158\/158298.jpg\/1000x1000.jpg<\/ImagePath>Vinyl<\/AlbumType><\/Album>2<\/IdAlbum>Nevermind<\/Name>Nirvana<\/Artist>12<\/NumberTracks>12.99<\/Price>150<\/Stock>http:\/\/assets.rollingstone.com\/assets\/images\/list\/d01d5961f477ace34b71b64df43da8cee246b68c.jpg<\/ImagePath>CD<\/AlbumType><\/Album>"}
然后我这样做是为了将响应发送到客户端

<?php

$client= new SoapClient('http://wvm067.dei.isep.ipp.pt/WebServices/WebServices.asmx?wsdl');
$params = array('APIKey'=>'A1638E93-A38B-422B-8324-C89B259EAA1C');
$result=$client->GetAllAlbums($params);
echo json_encode($result->GetAllAlbumsResult);

?>
问题是,在客户端,消息没有正确解析,因为它以{any…,然后以如下xml格式开头:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetAllAlbumsResponse xmlns="http://tempuri.org/">
      <GetAllAlbumsResult>
        <Album>
          <IdAlbum>int</IdAlbum>
          <Name>string</Name>
          <Artist>string</Artist>
          <NumberTracks>int</NumberTracks>
          <Price>double</Price>
          <Stock>int</Stock>
          <ImagePath>string</ImagePath>
          <AlbumType>string</AlbumType>
        </Album>
      </GetAllAlbumsResult>
    </GetAllAlbumsResponse>
  </soap:Body>
</soap:Envelope>
{"any":"1<\/IdAlbum>Wrath<\/Name>Lamb Of God<\/Artist>11<\/NumberTracks>7.29<\/Price>200<\/Stock>http:\/\/www.nuclearblast.de\/static\/articles\/158\/158298.jpg\/1000x1000.jpg<\/ImagePath>Vinyl<\/AlbumType><\/Album>2<\/IdAlbum>Nevermind<\/Name>Nirvana<\/Artist>12<\/NumberTracks>12.99<\/Price>150<\/Stock>http:\/\/assets.rollingstone.com\/assets\/images\/list\/d01d5961f477ace34b71b64df43da8cee246b68c.jpg<\/ImagePath>CD<\/AlbumType><\/Album>"}
这使得无法正常解析。有人知道发生了什么吗

编辑这里的Web服务

[WebMethod]
    public List<Album> GetAllAlbums(string APIKey)
    {
        //SQL Server Connection
        SqlConnection connection = CreateConnection();
        connection.Open();

        //DataSets
        DataSet albumsDataSet = new DataSet();

        //List to be returned
        List<Album> albums = new List<Album>();

        if (ValidateUser(connection, APIKey))
        {
            //SQL Query
            string query =
                "select a.Id, a.Name, a.Artist, a.NumberTracks, a.Price, a.Stock, a.ImagePath, al.Name as TypeAlbum " +
                "from Albums a join AlbumTypes al on a.AlbumTypeId = al.Id";

            //It means that the api key is valid
            SqlDataAdapter queryAlbums = new SqlDataAdapter(query, connection);
            queryAlbums.Fill(albumsDataSet);

            //Close DB's connection
            connection.Close();

            //Loop through all albums
            for (int albumCount = 0; albumCount < albumsDataSet.Tables[0].Rows.Count; albumCount++)
            {
                albums.Add(new Album()
                {
                    IdAlbum = Int32.Parse(albumsDataSet.Tables[0].Rows[albumCount]["Id"].ToString()),
                    Name = albumsDataSet.Tables[0].Rows[albumCount]["Name"].ToString(),
                    Artist = albumsDataSet.Tables[0].Rows[albumCount]["Artist"].ToString(),
                    NumberTracks = Int32.Parse(albumsDataSet.Tables[0].Rows[albumCount]["NumberTracks"].ToString()),
                    Price = Double.Parse(albumsDataSet.Tables[0].Rows[albumCount]["Price"].ToString()),
                    Stock = Int32.Parse(albumsDataSet.Tables[0].Rows[albumCount]["Stock"].ToString()),
                    ImagePath = albumsDataSet.Tables[0].Rows[albumCount]["ImagePath"].ToString(),
                    AlbumType = albumsDataSet.Tables[0].Rows[albumCount]["TypeAlbum"].ToString()
                });
            }
        }

        return albums;
    }
EDIT2这里是转储文件:

object(stdClass)#2 (1) {
["GetAllAlbumsResult"] =>
object(stdClass)#3 (1) {
["any"] => string(4056) "<Album>.....</Album>"

看起来GetAllAlbums方法返回一个字符串。php在这里没有做错任何事情。你必须自己拆分它或使用不同的策略来检索相册。不,GetAllAlbums返回一个列表。我用C自己做的。在php中列表意味着什么?我建议你转储它,你会看到它在php中是一个字符串。我用web服务。这意味着什么?我不怀疑你的c代码。但c是c而不是php。你如何将其输出转换为php?如果我理解正确,那么它就是http?http中没有类型,web中的一切都是字符串。所以我再次说:转储数据,看看它是什么类型。你为什么不按照建议那样做?