2016-07-25 37 views
-2

我有一個Web API,它與圖像一起返回一些信息(字節數組)ASP.Net MVC的Web API JavaScriptSerializer - 字節數組

var result = response.Result.Content.ReadAsStringAsync(); 

段:

{"CompanyName":"MyCompany","Address":"Address1","Logo":"iVBORw0KGgoAAAANSUhEUgAAAKAAAABQCAYAAACeXX40AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAW6gAAFuoB5Y5DEAAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNui8sowAAAAWdEVYdENyZWF0aW9uIFRpbWUAMDcvMjUvMTZmHO7IAAAB6HByVld4nO2WYVKDMBCFowkl3MIreBOv4A/xb6/DZTyAw128gUjFNsmyCRuglDrvSwdtl83blw1MPr8/vtRRHbuua9v+0zZN1zT9taeu67auT38UAAAAAAAAAAAAAABgIbbaaET0t5Kv4H+X/vvITf27Oyz5d8Xvaf9sCZcf1vq+0/5vJX9f/q3RJwz/bBTlb9iUEaliSC4Pcv/uchqD+lABU5yLliTi1U7Lj+tTAR1CouVjEKXZVo/CEv/eMOEMugjWpiRRWh4NZ/efTuDP0X8extG4ez9Z6p/RT1c3ET7vIKF/TkAn5XW6Op3WD+ULXt8kqyvSxRfT6z/hwD2FfHgiO8M/78Ap8NGJ7Jz+R/QX+c/Z/zv1/xe9Qv/J65e+/AbS+9+k/RvJ+qensEmDsmRJ/y37BvUecFahSodtTv/tIT5DrD8T26fK2f+cB+OHx8Wlsy8nBKH/8+lFLECyo4sj9e8fYDR3ADLs9Pwm9NJS+lRDfP4anfaDAs1dnf/ZlVhv7N7/lUe+f3e5on+JhLtHUlLknin/c91K88T9l003lh3nBfdg/8/3Hyzkuv7zpl5Qhtz/Urd8/oz+x8vwJYTlbr3/aVkRfQAAAAAAAAAAAAAAAAAA/C9e1Kt6V2/qST3fuhRwA34ABnmlFTLkCiwAAABIbWtCRvreyv4AAAAEAAAAAAAAAAAAAAAAA="} 

所以,當我嘗試映射

var profile = new JavaScriptSerializer().Deserialize<WebApi.Common.DomainObjects.WebProfile>(result); 

public class WebProfile 
    { 
     public string CompanyName { get; set; } 
     public string Address { get; set; } 
     public byte[] Logo { get; set; } 
    } 

我得到這個錯誤: 無法將類型的對象System.String爲鍵入「System.B yte []'

如果任何人都可以提供一個如何解決的例子會很好。

回答

-1

謝謝弗蘭克,我結束了剛剛創建一個新的屬性。

public byte[] Logo 
     { 
      get { return Convert.FromBase64String(LogoString); } 
     } 

public string LogoString { get; set; }