Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按键将JSON反序列化为C#对象_C#_Json_Deserialization - Fatal编程技术网

按键将JSON反序列化为C#对象

按键将JSON反序列化为C#对象,c#,json,deserialization,C#,Json,Deserialization,我在寻找一种反序列化JSON的方法,但有些键应该是值,这就是需要技巧的地方。我喜欢反序列化为: int valuetoExtract1 int valuetoExtract2 int valuetoExtract3 JSON如下所示: { "option1": "someOption1", "option12": "someOption2", "id": [ "12345" ], "filter": "someFilter", "

我在寻找一种反序列化JSON的方法,但有些键应该是值,这就是需要技巧的地方。我喜欢反序列化为:

int valuetoExtract1
int valuetoExtract2
int valuetoExtract3
JSON如下所示:

{
    "option1": "someOption1",
    "option12": "someOption2",
    "id": [
        "12345"
    ],
    "filter": "someFilter",
    "actualValues": [
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract1": "123"
        },
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract2": "234"
        },
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract3": "345"
        }
    ]
}
var jsonString = "{ 'option1': 'someOption1','option2': 'someOption2','id': ['12345'],'filter': 'someFilter','actualValues': [{'otherId': 24,'id': 12345,'valuetoExtract129': '123'},{'otherId': 24,'id': 12345,'valuetoExtract2': '234'},{'otherId': 24,'id': 12345,'valuetoExtract3': '345'}]}";

jsonString = Regex.Replace(jsonString, @"valuetoExtract[0-9]*", "valuetoExtract");
ApiResult result = JsonConvert.DeserializeObject<ApiResult>(jsonString);
你能试试这个吗

public class ActualValue
{
    public int otherId { get; set; }
    public int id { get; set; }
    public string valuetoExtract1 { get; set; }
    public string valuetoExtract2 { get; set; }
    public string valuetoExtract3 { get; set; }
}

public class RootObject
{
    public string option1 { get; set; }
    public string option12 { get; set; }
    public List<string> id { get; set; }
    public string filter { get; set; }
    public List<ActualValue> actualValues { get; set; }
}
公共类实际值
{
public int otherId{get;set;}
公共int id{get;set;}
public string valuetoExtract1{get;set;}
public string valuetoExtract2{get;set;}
public string valuetoExtract3{get;set;}
}
公共类根对象
{
公共字符串选项1{get;set;}
公共字符串选项12{get;set;}
公共列表id{get;set;}
公共字符串筛选器{get;set;}
公共列表实际值{get;set;}
}
你能试试这个吗:

public class ActualValue
{
    public int otherId { get; set; }
    public int id { get; set; }
    public string valuetoExtract1 { get; set; }
    public string valuetoExtract2 { get; set; }
    public string valuetoExtract3 { get; set; }
}

public class RootObject
{
    public string option1 { get; set; }
    public string option12 { get; set; }
    public List<string> id { get; set; }
    public string filter { get; set; }
    public List<ActualValue> actualValues { get; set; }
}
公共类实际值
{
public int otherId{get;set;}
公共int id{get;set;}
public string valuetoExtract1{get;set;}
public string valuetoExtract2{get;set;}
public string valuetoExtract3{get;set;}
}
公共类根对象
{
公共字符串选项1{get;set;}
公共字符串选项12{get;set;}
公共列表id{get;set;}
公共字符串筛选器{get;set;}
公共列表实际值{get;set;}
}

结尾是这样的,不是最漂亮的,而是迄今为止我能找到的最好的:


public class ActualValue
{
    public int valuetoExtract1 { get; set; }

}

ActualValues actualValues = new ActualValues();
JObject jsonO = JObject.Parse(json);
var converterJsonO = jsonO["actualValues"];
var oList = converterJsonO .Children();

foreach(JObject childObject in oList)
    {
        var d = childObject.ToObject<Dictionary<string, string>>();
        if (d.ContainsKey("valuetoExtract1")) { actualValues.valuetoExtract1= int.Parse(d["valuetoExtract1"]); }
    }

公共类实际值
{
public int valuetoExtract1{get;set;}
}
ActualValues ActualValues=新的ActualValues();
JObject jsonO=JObject.Parse(json);
var converterJsonO=jsonO[“实际值”];
var oList=converterJsonO.Children();
foreach(oList中的JObject子对象)
{
var d=childObject.ToObject();
if(d.ContainsKey(“valuetoExtract1”){actualValues.valuetoExtract1=int.Parse(d[“valuetoExtract1]”);}
}

结尾是这样的,不是最漂亮的,而是迄今为止我能找到的最好的:


public class ActualValue
{
    public int valuetoExtract1 { get; set; }

}

ActualValues actualValues = new ActualValues();
JObject jsonO = JObject.Parse(json);
var converterJsonO = jsonO["actualValues"];
var oList = converterJsonO .Children();

foreach(JObject childObject in oList)
    {
        var d = childObject.ToObject<Dictionary<string, string>>();
        if (d.ContainsKey("valuetoExtract1")) { actualValues.valuetoExtract1= int.Parse(d["valuetoExtract1"]); }
    }

公共类实际值
{
public int valuetoExtract1{get;set;}
}
ActualValues ActualValues=新的ActualValues();
JObject jsonO=JObject.Parse(json);
var converterJsonO=jsonO[“实际值”];
var oList=converterJsonO.Children();
foreach(oList中的JObject子对象)
{
var d=childObject.ToObject();
if(d.ContainsKey(“valuetoExtract1”){actualValues.valuetoExtract1=int.Parse(d[“valuetoExtract1]”);}
}

您可以使用以下方法准备词典:

        static Dictionary<string, string> GetExtracts(string jsonText)
        {
            var valuetoExtractList = new Dictionary<string, string>();
            using (var reader = new JsonTextReader(new StringReader(jsonText)))
            {
                while (reader.Read())
                {
                    if (reader.TokenType.ToString().Equals("PropertyName")
                       && reader.ValueType.ToString().Equals("System.String")
                       && reader.Value.ToString().StartsWith("valuetoExtract"))
                    {
                        var key = reader.Value.ToString();
                        reader.Read();
                        valuetoExtractList.Add(key, reader.Value.ToString());
                    }
                }
            }

            return valuetoExtractList;
        }
静态字典GetExtracts(字符串jsonText)
{
var valuetoExtractList=新字典();
使用(var reader=newjsontextreader(newstringreader(jsonText)))
{
while(reader.Read())
{
if(reader.TokenType.ToString().Equals(“PropertyName”)
&&reader.ValueType.ToString().Equals(“System.String”)
&&reader.Value.ToString().StartsWith(“valuetoExtract”))
{
var key=reader.Value.ToString();
reader.Read();
valuetoExtractList.Add(key,reader.Value.ToString());
}
}
}
返回值到ExtractList;
}

根据

改编您可以使用以下方法准备词典:

        static Dictionary<string, string> GetExtracts(string jsonText)
        {
            var valuetoExtractList = new Dictionary<string, string>();
            using (var reader = new JsonTextReader(new StringReader(jsonText)))
            {
                while (reader.Read())
                {
                    if (reader.TokenType.ToString().Equals("PropertyName")
                       && reader.ValueType.ToString().Equals("System.String")
                       && reader.Value.ToString().StartsWith("valuetoExtract"))
                    {
                        var key = reader.Value.ToString();
                        reader.Read();
                        valuetoExtractList.Add(key, reader.Value.ToString());
                    }
                }
            }

            return valuetoExtractList;
        }
静态字典GetExtracts(字符串jsonText)
{
var valuetoExtractList=新字典();
使用(var reader=newjsontextreader(newstringreader(jsonText)))
{
while(reader.Read())
{
if(reader.TokenType.ToString().Equals(“PropertyName”)
&&reader.ValueType.ToString().Equals(“System.String”)
&&reader.Value.ToString().StartsWith(“valuetoExtract”))
{
var key=reader.Value.ToString();
reader.Read();
valuetoExtractList.Add(key,reader.Value.ToString());
}
}
}
返回值到ExtractList;
}

根据

进行调整,您可以将JSON结果作为一个字符串,因此您可以更轻松地从
值中删除所有数字以提取
。为此,您可以使用
System.Text.RegularExpressions
命名空间中的
Regex.Replace()
。然后,您可以为
valuetoExtract
键使用单个属性

因此,您的代码可以如下所示:

{
    "option1": "someOption1",
    "option12": "someOption2",
    "id": [
        "12345"
    ],
    "filter": "someFilter",
    "actualValues": [
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract1": "123"
        },
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract2": "234"
        },
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract3": "345"
        }
    ]
}
var jsonString = "{ 'option1': 'someOption1','option2': 'someOption2','id': ['12345'],'filter': 'someFilter','actualValues': [{'otherId': 24,'id': 12345,'valuetoExtract129': '123'},{'otherId': 24,'id': 12345,'valuetoExtract2': '234'},{'otherId': 24,'id': 12345,'valuetoExtract3': '345'}]}";

jsonString = Regex.Replace(jsonString, @"valuetoExtract[0-9]*", "valuetoExtract");
ApiResult result = JsonConvert.DeserializeObject<ApiResult>(jsonString);

JSON结果是一个字符串,因此从
valuetoExtract
中删除所有数字可能更容易。为此,您可以使用
System.Text.RegularExpressions
命名空间中的
Regex.Replace()
。然后,您可以为
valuetoExtract
键使用单个属性

因此,您的代码可以如下所示:

{
    "option1": "someOption1",
    "option12": "someOption2",
    "id": [
        "12345"
    ],
    "filter": "someFilter",
    "actualValues": [
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract1": "123"
        },
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract2": "234"
        },
        {
            "otherId": 24,
            "id": 12345,
            "valuetoExtract3": "345"
        }
    ]
}
var jsonString = "{ 'option1': 'someOption1','option2': 'someOption2','id': ['12345'],'filter': 'someFilter','actualValues': [{'otherId': 24,'id': 12345,'valuetoExtract129': '123'},{'otherId': 24,'id': 12345,'valuetoExtract2': '234'},{'otherId': 24,'id': 12345,'valuetoExtract3': '345'}]}";

jsonString = Regex.Replace(jsonString, @"valuetoExtract[0-9]*", "valuetoExtract");
ApiResult result = JsonConvert.DeserializeObject<ApiResult>(jsonString);

到目前为止,您尝试了什么?尝试了将JsonConvert.DeserializeObject转换为预生成的类,但我得到了一个新对象,其中包含每个valuetoExtract的所有变量,与valuetoExtrace get value匹配的变量,其余为null,还尝试了jObject并反序列化到Dictionary,但它不起作用。到目前为止,您尝试了什么?尝试了JsonConvert.DeserializeObject到预生成的类,但我得到了一个新对象,其中每个值都有所有变量用于Extract,与valuetoExtrace get value匹配的变量,其余为null,还尝试了jObject并反序列化到Dictionary,但它不会像我那样工作,但actualValues列表中的每个actualValues对象都有完整范围的actualValues属性,这有点不可管理,如果要获取特定属性的值,必须从列表中选择正确的对象。所以,如果JSON消息的序列发生变化,您不知道应该像oll ofI那样选择哪一个,但actualValues列表中的每个ActualValue对象都具有完整范围的ActualValue属性,这有点不可管理。如果您想获得特定属性的值,您必须从列表中选择正确的对象。所以,若JSON消息的顺序发生了变化,那个么您就不会改变