Recently a lot of API to access, and most will not have the code example, only the request body json format, as follows:
So we own transmission request, data is generally required JSON achieved by their dynamic code, such as requesting the following exam proofing the API , request code as follows:
private class Body
{
public List<Data> data;
}
private class Data
{
public string id = "";
public string item = "";
public string analytic = "";
public List<string> answer;
public string materialContent = "";
public List<string> option;
public int subject;
}
IEnumerator Correct_Item()
{
Body body = new Body();
body.data = new List<Data>();
Data data = new Data();
data.id = "100651";
data.item = "下列不属于墨子思想的是()";
data.analytic = "“克已复礼”是孔子思想";
data.materialContent = "null";
data.answer = new List<string>();
data.answer.Add("D");
data.option = new List<string>();
data.option.Add("A");
data.option.Add("B");
data.option.Add("C");
data.option.Add("D");
data.subject = 1;
body.data.Add(data);
string requestdata = JsonMapper.ToJson(body);
Debug.Log(requestdata);
byte[] postBytes = Encoding.UTF8.GetBytes(requestdata);
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("Content-Type", "application/json");
WWW www = new WWW("http://openapiai.xueersi.com/v1/api/nlp/nlp/correct_item?app_key=自己的app_key", postBytes,header);
yield return www;
if(www.isDone)
{
if(www.error==null)
{
Debug.Log(www.text);
}
}
}
json request body are as follows: Request the following results:
In addition to manually write the code, there is a very simple way, you can json data into object types, http://www.bejson.com/convert/json2csharp/ , results as shown below: