如何处理JSON的同时返回一个字符串,字符串数组? [英] How to handle json that returns both a string and a string array?

查看:230
本文介绍了如何处理JSON的同时返回一个字符串,字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是雅虎幻想体育API。我得到的结果是这样的:

I'm using the Yahoo fantasy sports api. I'm getting a result like this:

"player": [
    {
        ...
        "eligible_positions": {
            "position": "QB"
        },
        ...
    },
    {
        ...
        "eligible_positions": {
            "position": [
                "WR",
                "W/R/T"
            ]
        },
        ...
    },

它是如何,我可以反序列化呢?

How is it that I can deserialize this?

我的code是这样的:

My code looks like this:

var json = new JavaScriptSerializer();

if (response != null)
{
    JSONResponse JSONResponseObject = json.Deserialize<JSONResponse>(response);
    return JSONResponseObject;
}

在我JSONResponse.cs文件:

And in my JSONResponse.cs file:

public class Player
{
    public string player_key { get; set; }
    public string player_id { get; set; }
    public string display_position { get; set; }        
    public SelectedPosition selected_position { get; set; }
    public Eligible_Positions eligible_positions { get; set; }
    public Name name { get; set; }            
}


public class Eligible_Positions
{        
    public string position { get; set; }
}

当我运行这一点,因为eligible_positions便可返回一个字符串,字符串数组,我不断收到错误类型'System.String'不支持数组的反序列化。

When I run this, since eligible_positions can return both a string and a string array, I keep getting the error "Type 'System.String' is not supported for deserialization of an array".

我也试着转动公共字符串位置{搞定;组; } 公共字符串[]位置{搞定;组; } ,但我仍然得到一个错误。

I've also tried turning public string position { get; set; } to public string[] position { get; set; } but I still get an error.

我应该如何处理呢?

推荐答案

我将使用 Json.Net 。我们的想法是:申报位置列表&LT;串&GT; ,如果在JSON值是串。然后将其转换为一个列表

I'll use Json.Net. The idea is: "declare position as a List<string> and if the value in json is a string. then convert it to a List"

code反序列化

var api = JsonConvert.DeserializeObject<SportsAPI>(json);

JsonConverter

public class StringConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
    {

        if(reader.ValueType==typeof(string))
        {
            return new List<string>() { (string)reader.Value };
        }
        return serializer.Deserialize<List<string>>(reader);
    }

    public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

样品的Json

{
    "player": [
        {
            "eligible_positions": {
                "position": "QB"
            }
        },
        {
            "eligible_positions": {
                "position": [
                    "WR",
                    "W/R/T"
                ]
            }
        }
    ]
}   

类(简体版)

public class EligiblePositions
{
    [JsonConverter(typeof(StringConverter))] // <-- See This
    public List<string> position { get; set; }
}

public class Player
{
    public EligiblePositions eligible_positions { get; set; }
}

public class SportsAPI
{
    public List<Player> player { get; set; }
}

这篇关于如何处理JSON的同时返回一个字符串,字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆