2014-04-01 126 views
0

荫使用Newtonsoft.Json格式化的Json

对于这个我下面的代码,

[WebMethod] 
public static string GetData() 
{ 
    string msg = string.Empty; 
    string json; 

    using (MySqlConnection con = new MySqlConnection("Server=localhost;Database=working_sample;uid=root;[email protected];pooling=false;")) 
    { 
     using (MySqlCommand cmd = new MySqlCommand("select * from json_example_table where subject='Swe'", con)) 
     { 
      con.Open(); 
      cmd.ExecuteScalar(); 
      MySqlDataAdapter da = new MySqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 

      json = JsonConvert.SerializeObject(ds.Tables[0]); 
      // Using this I am getting single letter as output 
      // IN ASPX Code is as below..... 
      success: function (data) { 
       var objdata = $.parseJSON(data.d); 
       var obj = data.d; 
       $('#txtname').val(obj[0]); // This is giving "[" as output 


    //var settings = new JsonSerializerSettings { Formatting = Formatting.Indented }; 
    //string jsonn = JsonConvert.SerializeObject(ds, Formatting.None, settings); 
     con.Close(); 
     } 
    } 

    return json; 
} 

什么我现在做的就是正确的输出... 我试图与格式,但它给错误作为

错误

ambiguous ref btwn System.Xml.Formatting and System.Json.Formatting 

任何帮助请

回答

0

因为您正在使用Newtonsoft.Json请使用正确的命名空间。 Formatting属于Newtonsoft.Json命名空间,请参阅Documentation

尝试用指定如Newtonsoft.Json.Formatting

+0

但namesapce名Json的系统中存在多年平均值..命名空间,这是错误 – Pink

+0

@Pink,更新了我的答案 –

+0

Newtonsoft.Json.Formatting.JsonSerializerSettings不包含格式定义 – Pink