在PHP中引入Json时出错

时间:2014-12-07 18:05:42

标签: 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/">
  <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>

然后我这样做将响应发送给客户端。

<?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格式,如下所示:

{"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>"}

这使得无法正常解析。有人知道它发生了什么吗?

编辑这是网络服务。

[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>"

0 个答案:

没有答案