试图解析RESTful api响应

时间:2021-10-30 21:56:29

I have a problem, and I can't seem to find any clue on google about how to solve it. Im trying to parse the response of a RESTful-api service.

我有一个问题,我似乎无法在谷歌找到任何关于如何解决它的线索。我试图解析RESTful-api服务的响应。

[{"account_id":"5401585","history":"84967869|2|03\/30\/2012,84972342|2|03\/30\/2012,85312563|2|04\/02\/2      012,85314831|2|04\/02\/2012,85318847|2|04\/02\/2012,85435388|2|04\/03\/2012,100244102|2|09\/09\/2012,100     245865|2|09\/09\/2012,100249440|2|09\/09\/2012,100251434|2|09\/09\/2012"}]'

I don't understand how I can have this response put to a List as my model says.

正如我的模型所说,我不明白如何将这个响应放到List中。

Basically, I want every line which is divided by the "," added to a list.

基本上,我希望将每行除以“,”添加到列表中。

My code looks like this:

我的代码如下所示:

    public class MatchHistoryParser
      {
       public RootObject get()
       {
        using (var webClient = new System.Net.WebClient())
        {

            string URL2 =       @"api url";
            var json = webClient.DownloadString(URL2);
            RootObject match = JsonConvert.DeserializeObject<RootObject>(json);

            return match;
        }
    }
}
public class RootObject
{
    public string account_id { get; set; }
    public string history { get; set; }
    public string win_loss_history { get; set; }
}

Throws error:" Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'HoNEnemy.BL.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly."

引发错误:“附加信息:无法将当前JSON数组(例如[1,2,3])反序列化为类型'HoNEnemy.BL.RootObject',因为该类型需要JSON对象(例如{”name“:”value“} )正确反序列化。“

2 个解决方案

#1


1  

You are getting a list, not an object, so you should parse like this

你得到的是一个列表,而不是一个对象,所以你应该像这样解析

RootObject rootObject;
var matches = JsonConvert.DeserializeObject<IList<RootObject>>(json);

then

然后

if(matches.Any())
{
    rootObject = matches[0];
}

And then create a method to split the history string into a list and use that, not the property

然后创建一个方法将历史字符串拆分为一个列表并使用它,而不是属性

#2


2  

Use JSON.Net to Deserialize your response (examples provided on this website) then use Split() and ToList() on your History field.

使用JSON.Net反序列化您的响应(本网站提供的示例),然后在History字段中使用Split()和ToList()。

You can replace JSON.Net with JavaScriptSerializer if you don't want to use an external lib.

如果您不想使用外部库,可以使用JavaScriptSerializer替换JSON.Net。

#1


1  

You are getting a list, not an object, so you should parse like this

你得到的是一个列表,而不是一个对象,所以你应该像这样解析

RootObject rootObject;
var matches = JsonConvert.DeserializeObject<IList<RootObject>>(json);

then

然后

if(matches.Any())
{
    rootObject = matches[0];
}

And then create a method to split the history string into a list and use that, not the property

然后创建一个方法将历史字符串拆分为一个列表并使用它,而不是属性

#2


2  

Use JSON.Net to Deserialize your response (examples provided on this website) then use Split() and ToList() on your History field.

使用JSON.Net反序列化您的响应(本网站提供的示例),然后在History字段中使用Split()和ToList()。

You can replace JSON.Net with JavaScriptSerializer if you don't want to use an external lib.

如果您不想使用外部库,可以使用JavaScriptSerializer替换JSON.Net。