我有這樣的結構..如何從Web API接收此Json?
Cliente側< - >網絡API接待< - >網絡API返回 - >數據庫
所以,當我的網頁API返回給我的JSON,我不知道如何獲得結果。
我沒有顯示與數據庫的連接。
在Web Api Front上,行var data = response.Content.ReadAsStringAsync().Result;
爲我提供了具有所有正確值的Json數組。但我不知道如何將它發送到客戶端上的ApiCall。
我必須把什麼回報?
[服務調用網絡API前]
public class ApiCall
{
static readonly string ApiUrl = "http://Localhost:1762/api/{0}/{1}?nomePesquisa={2}&nomeMae={3}&nomePai={4}&dtNasc={5}&nrg={6}&ncpf={7}";
//buscar os dados com os parametros digitados
public async Task<T> GetResponse<T>(string controller, string method, string nomepesquisa,
string nomemae, string nomepai, string dtnasc, string nrg, string ncpf) where T : class
{
var client = new System.Net.Http.HttpClient();
//Definide o Header de resultado para JSON, para evitar que seja retornado um HTML ou XML
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var urllink = string.Format(ApiUrl, controller, method, nomepesquisa, nomemae, nomepai, dtnasc, nrg, ncpf);
var response = await client.GetAsync(urllink);
//Lê a string retornada
var JsonResult = response.Content.ReadAsStringAsync().Result;
if (typeof(T) == typeof(string))
return null;
//Converte o resultado Json para uma Classe utilizando as Libs do Newtonsoft.Json
var rootobject = JsonConvert.DeserializeObject<T>(JsonResult);
return rootobject;
}
}
[網絡API前調用網絡API返回]
[HttpGet]
[Route("Envolvidos")]
public IEnumerable<Envolvido> GetEnvolv(string nomePesquisa, string nomeMae, string nomePai, string dtNasc
, string nRG, string nCPF)
{
DataSet lretorno = new DataSet();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://Localhost:1762/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Usage
HttpResponseMessage response = client.GetAsync("api/nomes/Envolvidos?nomePesquisa="+nomePesquisa+"&nomeMae="+nomeMae+"&nomePai="+nomePai+"&dtNasc="+dtNasc+"&nrg="+nRG+"&ncpf="+nCPF+"").Result;
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStringAsync().Result;
//Envolvido envolvidoResult = JsonConvert.DeserializeObject<Envolvido>(data);
return data.AsEnumerable().Select(row => new Envolvido
{
});
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
//return WHATTTTT?? ;
}
}
[的WebAPI返回]
[HttpGet]
[Route("Envolvidos")]
public IEnumerable<Envolvido> GetEnvolv(string nomePesquisa, string nomeMae, string nomePai, string dtnasc
, string nRG, string nCPF)
{
DataSet lretorno = new DataSet();
string connectionString = GetConnectionString();
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
OracleDataReader reader = null;
OracleCommand cmd = new OracleCommand();
cmd.Connection = connection;
cmd = new OracleCommand("MOBILE.XAPIMANDADOMOBILE.BUSCAPOSSIVEISCANDIDATOSPF", connection);
cmd.CommandType = CommandType.StoredProcedure;
//variáveis entrada
cmd.Parameters.Add(new OracleParameter("ivnome",nomePesquisa));
cmd.Parameters.Add(new OracleParameter("ivmae", nomeMae));
cmd.Parameters.Add(new OracleParameter("ivpai", nomePai));
cmd.Parameters.Add(new OracleParameter("ivdatanasc", dtnasc));
cmd.Parameters.Add(new OracleParameter("ivrg", nRG));
cmd.Parameters.Add(new OracleParameter("icpf", nCPF));
//variáveis de saida
cmd.Parameters.Add(new OracleParameter("oretorno", OracleDbType.RefCursor)).Direction = ParameterDirection.Output;
cmd.Parameters.Add(new OracleParameter("qretorno", OracleDbType.RefCursor)).Direction = ParameterDirection.Output;
connection.Open();
cmd.ExecuteNonQuery();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
//CRIO A LISTA
lretorno.Load(reader, LoadOption.OverwriteChanges, "BUSCAPOSSIVEISCANDIDATOSPF");
connection.Close();
connection.Dispose();
//CARREGO O DATASET E TRANSFORMO PARA IENUMERABLE E RETORNO SEUS VALORES PRO JSON
return lretorno.Tables[0].AsEnumerable().Select(row => new Envolvido
{
SUSPID = Convert.ToInt32(row["SUSPID"]),
IVNOME = Convert.ToString(row["SUSPNOME"]),
IVMAE = Convert.ToString(row["SUSPMAE"]),
IVPAI = Convert.ToString(row["SUSPPAI"]),
IVDATANASC = Convert.ToString(row["SUSPDATANASC"]).Replace(" 00:00:00", ""),
IVRG = Convert.ToString(row["RG"]),
ICPF = Convert.ToString(row["CPF"]),
MANDADO = Convert.ToInt16(row["TEMMANDADO"]),
OCORRENCIA = Convert.ToInt16(row["TEMOCORRENCIA"]),
});
}
}
你需要做任何更改Web API Front中的結果發送給客戶端之前? – Nkosi
@Nkosi是的!使用此行返回data.AsEnumerable()。Select(row => new Envolvido { }); ,我得到所有值0或null,因爲這個選擇是錯誤的 –
@Nkosi另外,我還有一個返回data.AsEnumerable()。選擇(行=>新Envolvido {SUSPID = Convert.ToInt32(「SUSPID」)) ,IVNOME = Convert.ToString(「SUSPNOME」),IVMAE = Convert.ToString(「SUSPMAE」),IVPAI = Convert.ToString(「SUSPPAI」),IVDATANASC = Convert.ToString(「SUSPDATANASC」)。Replace(「00: IVRG = Convert.ToString(「RG」),ICPF = Convert.ToString(「CPF」),MANDADO = Convert.ToInt16(「TEMMANDADO」),OCORRENCIA = Convert.ToInt16(「TEMOCORRENCIA 「),});但我得到一個錯誤System.FormatException被用戶代碼未處理HResult = -2146233033 –