【发布时间】:2016-05-02 18:47:25
【问题描述】:
我只是想从 .net 网络服务中获取 json
在安卓应用中使用
我的网络方法是……
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class GetFlash : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetOneFlash()
{
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["eng_lang_tutConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand sqlCommand =
new SqlCommand(@"SELECT * FROM focus WHERE P_ID = " + HttpContext.Current.Request.QueryString["IndexOrder"], con);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter jsonWriter = new JsonTextWriter(sw);
try
{
SqlDataReader reader = sqlCommand.ExecuteReader();
reader.Read();
int fieldcount = reader.FieldCount; // count how many columns are in the row
object[] values = new object[fieldcount]; // storage for column values
reader.GetValues(values); // extract the values in each column
jsonWriter.WriteStartObject();
for (int index = 0; index < fieldcount; index++)
{
jsonWriter.WritePropertyName(reader.GetName(index)); // column name
jsonWriter.WriteValue(values[index]); // value in column
}
jsonWriter.WriteEndObject();
reader.Close();
}
catch (SqlException sqlException)
{
con.Close();
return ("No data fetched ..." + "\n" + "---------------------------");
}
finally
{
con.Close();
}
return sb.ToString();
}
}
我的网络服务变得混乱 - 既不是 json 也不是 xml http://englishflash.somee.com/WebService/GetFlash.asmx/GetOneFlash?IndexOrder=1
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://tempuri.org/">
{"rownumber":1,"P_ID":120,"F_Eword":"a.m. ","F_Aword":"صباحا ","F_Notes":"","Usage":"","F_pic":null,"F_pronounce":"https://ssl.gstatic.com/dictionary/static/sounds/de/0/a.m..mp3"}
</string>
最大的突破是我得到了这样的 android HTTPrequest 结果
<HTML></HTML>
我所需要的只是来自 .net 网络服务的 json 像这样
https://api.github.com/user/3bdoelnaggar
【问题讨论】:
-
我不明白为什么你需要使用 JsonWriter 来收缩 json 对象
-
您可以返回任何对象并将 Json 标记为响应,它会处理所有事情;无需手动准备 Json 字符串
-
我不知道获取 json 对象的正确方法是使用 JsonWriter 还是其他任何我需要的东西 api.github.com/user/3bdoelnaggar
标签: c# json web-services