我目前正在为API创建一个类库。这是我目前的问题:
API支持多调用功能,我可以以数组的形式同时获取多个提交的数据(即外观,模型,脚本等),每个数组都有一个对象提交。我将提供的链接完全用于测试目的,因为它返回相同的提交三次,并且可以找到here。
目标是获取每个对象的原始JSON数据并将其放入字符串数组中,以便我可以遍历每个对象并单独反序列化它们。原因是api可以返回任何类型的对象(如上所述),并且该系统必须调整到通过它的每个对象。这是当前的代码:
Program.cs的
class Program
{
static WebClient web = new WebClient();
static void Main(string[] args)
{
APICaller ApiManager = new APICaller(web);
Model exampleObject = new Model(3962);
Model exampleObject2 = new Model(3962);
Model exampleObject3 = new Model(3962);
List<Model> SubmissionInstances = new List<Model>() { exampleObject, exampleObject2, exampleObject3 };
int[] IDs = new int[] { exampleObject.itemID, exampleObject2.itemID, exampleObject3.itemID };
string[] fields = new string[] { exampleObject.fields, exampleObject2.fields, exampleObject3.fields };
ApiManager.Data(SubmissionInstances, IDs, fields);
}
}
APICaller.cs
public List<object> Data<T>(List<T> type, int[] itemid, string[] fields)
{
//Same as the first function but supports multi-call
string itemTypes = "";
string itemIDs = "";
string itemFields = "";
string finalURL = "http://api.gamebanana.com/Core/Item/Data?";
int i = 0;
foreach (object obj in type)
{
itemTypes += "&itemtype[]=" + obj.GetType().Name;
types[i] = obj.GetType();
i++;
}
foreach (int ID in itemid)
{
itemIDs += "&itemid[]=" + ID;
}
foreach (string field in fields)
{
itemFields += "&fields[]=" + field;
}
finalURL += itemTypes + itemIDs + itemFields + "&return_object=1";
string[] JSONObjects = JsonConvert.DeserializeObject<string[]>(client.DownloadString(finalURL));
List<object> toReturn = new List<object>();
i = 0;
foreach (string str in JSONObjects)
{
toReturn.Add(DeserializeObject(type[i], str));
i++;
}
return toReturn;
}
public T DeserializeObject<T> (T obj, dynamic JSONObj)
{
return JsonConvert.DeserializeObject<T>(JSONObj);
}