2014-11-06 96 views
0

iam新增了wcf。 「:\」 Sathyam IAM通過使用下面的代碼如何在wcf中返回json格式

public string ConvertDataTabletoString() 
    { 
     DataTable dt = new DataTable(); 
     using (SqlConnection con = new SqlConnection("Data Source=mar-pc;database=user;User ID=sa;Password=123123;")) 
     { 
      using (SqlCommand cmd = new SqlCommand("select title=tname,tid=taddress from userdetails where userid='1'", con)) 
      { 
       con.Open(); 
       SqlDataAdapter da = new SqlDataAdapter(cmd); 
       da.Fill(dt); 
       System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
       List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); 
       Dictionary<string, object> row; 
       foreach (DataRow dr in dt.Rows) 
       { 
        row = new Dictionary<string, object>(); 
        foreach (DataColumn col in dt.Columns) 
        { 
         row.Add(col.ColumnName, dr[col]); 
        } 
        rows.Add(row); 
       } 
       return serializer.Serialize(rows); 
      } 
     } 
    } 

其中IAM獲得結果作爲

「[{\」 標題\由序列化爲JSON格式顯影WCF寧靜

IAM返回字符串\ 「\ 」TID \「:\ 」尼扎馬巴德\「}]」

我想要得到的結果作爲

{ 「稱號」: 「Sathyam」, 「TID」: 「尼扎馬巴德」}

IAM承包經營和

[OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "MyGetData/{value}/{password}")] 

幫助我,我試圖bodystyle如裹也沒有

+0

但是您有'List '。那是你的輸出。你爲什麼要改變它? – 2014-11-06 08:44:26

+0

我想得到的輸出爲{「title」:「Sathyam」,「tid」:「Nizamabad」}所以我應該怎麼做不知道如何輸出將是 – user2765331 2014-11-06 08:49:26

回答

0

你方brakets besause序列化的詞典列表,而不是一個單一的對象。

也許轉義字符的問題是雙重序列化:您已經使用ResponseFormat = WebMessageFormat.Json並在convertion方法中序列化字典。 看看這個問題:How do I return clean JSON from a WCF Service?

+0

我在瀏覽器中得到這個輸出怎麼辦我得到了轉義字符 – user2765331 2014-11-06 09:00:48

+0

@ user2765331,我更新了答案 - 問題可以通過雙JSON序列化 – ZuoLi 2014-11-06 09:15:32

+1

您有方括號不是因爲您序列化字典,而是因爲您序列化字典列表。所有數組,列表等都用方括號序列化。 – rraszewski 2014-11-06 09:17:24

0

ConvertDataTabletoString()方法調用WCF服務方法嗎?如果是,那麼你不必手動序列化List<Directory>,因爲它是WCF框架的工作。

在我看來你的代碼應該是:

public List<Dictionary<string, object>> ConvertDataTabletoString() 
{ 
    // your prev code ... 
    return rows;    
} 

而在WCF服務方法,你應該只返回List<Dictionary<string, object>

0

您可以使用下面的方法來解決烏爾問題:

[ServiceContract] 
    public interface IService 
    { 

[WebGet(UriTemplate = "GetStates", ResponseFormat = WebMessageFormat.Json)] 
     [OperationContract] 
     List<ServiceData> GetStates(); 
    } 

And in Service.cs 

     public List<ServiceData> GetStates() 
     { 
      using (var db = new local_Entities()) 
      { 

       var statesList = db.States.ToList(); 

       return statesList.Select(state => new ServiceData 
       { 
        Id = state.StateId, Value = state.StateName 
       }).OrderBy(s => s.Value).ToList(); 

      } 
     } 

Note you have to just return the return type collection, at my end I have a List of ServiceData 
    public class ServiceData 
    { 
     public int Id { get; set; } 
     public string Value { get; set; } 
    } 
0

我在這裏得到了答案Returning raw json (string) in wcf

將字符串的返回類型更改爲流,並在末尾添加此部分

WebOperationContext.Current.OutgoingResponse.ContentType ="application/json; charset=utf-8"; 
    return new MemoryStream(Encoding.UTF8.GetBytes(serializer.Serialize(rows)));