反序列化后如何过滤此 json?
how do i filter this json after deserialization?
responsestring =
"{\"resultList\":
[{\"modelId\":11,\"modelName\":\"indvsves12\",\"modelLang\":\"en-US\",\"modelVersion\":6,\"scoreMap\":{\"individual\":0.5,\"vessel\":0.5},\"bestCategory\":\"vessel\"}]}"
如何在反序列化后过滤此 json?
我的反序列化代码是
responsestring = response.Content.ReadAsStringAsync().Result;
ClassifierResponse Response = JsonConvert.DeserializeObject<ClassifierResponse>(responsestring);
请说明您需要什么过滤器,否则,这是一个广泛的问题。
假设您正在 ResultList 上寻找过滤器,因为这是我能看到的唯一可行的选项。反序列化后,您可以像使用任何其他 LINQ 对象一样使用它。例如(代码未测试):
var x = Response.ResultList.Where(d => d.ModelName == "indvsves12").ToList();
不确定您是想要对象还是特定的 属性 值。所以提供这两个例子。
string responsestring ="{\"resultList\": [{\"modelId\":11,\"modelName\":\"indvsves12\",\"modelLang\":\"en - US\",\"modelVersion\":6,\"scoreMap\":{\"individual\":0.5,\"vessel\":0.5},\"bestCategory\":\"vessel\"}]}";
//responsestring = response.Content.ReadAsStringAsync().Result;
ClassifierResponse Response = JsonConvert.DeserializeObject<ClassifierResponse>(responsestring);
ResultList filObject = Response.Resultlist.Find(x => x.ModelId == 11);
string modelName = Response.Resultlist.Find(x => x.ModelId == 11).ModelName;
您可以使用 LINQ 也可以查询
var x = from x in Response.ResultList where x.ModelName == "indvsves12" select x.ModelName ;
使用foreach
提取多条记录。
编辑:
作为 commnent 中的讨论,您需要从 scoremap
获取记录
你在反序列化中遇到错误
首先从
更新您的class
public class Scoremap
{
public double Individual { get; set; }
public double Vessel { get; set; }
}
到
public class Scoremap
{
public double Individual { get; set; }
public double Vessel { get; set; }
public string bestCategory { get; set; }
}
它将解决错误反序列化
那么你就可以使用
var x = from x in Response.ResultList where x.ModelName == "indvsves12" select x;
foreach(var items in x)
{
string val1 = item.scoreMap.individual;
string val2 = item.scoreMap.vessel;
string val3 = item.scoreMap.bestCategory;
}
我自己找到了答案我可以像这样访问响应值:Response.ResultList[0].Scoremap.Indivdual.
responsestring =
"{\"resultList\":
[{\"modelId\":11,\"modelName\":\"indvsves12\",\"modelLang\":\"en-US\",\"modelVersion\":6,\"scoreMap\":{\"individual\":0.5,\"vessel\":0.5},\"bestCategory\":\"vessel\"}]}"
如何在反序列化后过滤此 json? 我的反序列化代码是
responsestring = response.Content.ReadAsStringAsync().Result;
ClassifierResponse Response = JsonConvert.DeserializeObject<ClassifierResponse>(responsestring);
请说明您需要什么过滤器,否则,这是一个广泛的问题。
假设您正在 ResultList 上寻找过滤器,因为这是我能看到的唯一可行的选项。反序列化后,您可以像使用任何其他 LINQ 对象一样使用它。例如(代码未测试):
var x = Response.ResultList.Where(d => d.ModelName == "indvsves12").ToList();
不确定您是想要对象还是特定的 属性 值。所以提供这两个例子。
string responsestring ="{\"resultList\": [{\"modelId\":11,\"modelName\":\"indvsves12\",\"modelLang\":\"en - US\",\"modelVersion\":6,\"scoreMap\":{\"individual\":0.5,\"vessel\":0.5},\"bestCategory\":\"vessel\"}]}";
//responsestring = response.Content.ReadAsStringAsync().Result;
ClassifierResponse Response = JsonConvert.DeserializeObject<ClassifierResponse>(responsestring);
ResultList filObject = Response.Resultlist.Find(x => x.ModelId == 11);
string modelName = Response.Resultlist.Find(x => x.ModelId == 11).ModelName;
您可以使用 LINQ 也可以查询
var x = from x in Response.ResultList where x.ModelName == "indvsves12" select x.ModelName ;
使用foreach
提取多条记录。
编辑:
作为 commnent 中的讨论,您需要从 scoremap
获取记录
你在反序列化中遇到错误
首先从
更新您的class public class Scoremap
{
public double Individual { get; set; }
public double Vessel { get; set; }
}
到
public class Scoremap
{
public double Individual { get; set; }
public double Vessel { get; set; }
public string bestCategory { get; set; }
}
它将解决错误反序列化
那么你就可以使用
var x = from x in Response.ResultList where x.ModelName == "indvsves12" select x;
foreach(var items in x)
{
string val1 = item.scoreMap.individual;
string val2 = item.scoreMap.vessel;
string val3 = item.scoreMap.bestCategory;
}
我自己找到了答案我可以像这样访问响应值:Response.ResultList[0].Scoremap.Indivdual.