【发布时间】:2019-01-18 11:07:08
【问题描述】:
我从第三方获取 json 格式的 json 数据。
在某些情况下,我试图从“Id”中获取RollId,从“Data”中获取MType
“数据”没有字段,有点空白。
当我们有“数据”时它就起作用了。在空白的情况下它不起作用。 对此有任何想法吗?有没有更好的方法来做到这一点?
这是 Json
string json = @"{
'root': {
'_type': '_container',
'Class': '.key.PModel',
'Elements': {
'_type': 'array<element>',
'_data': [
{
'_type': 'element',
'Class': '.key.PElement',
'Id': {
'_type': 'testId',
'Class': '.key.PModel',
'RollId': '.key.7157'
},
'Data': {
'_type': 'p_model',
'Class': '.key.Unsupported',
'MType': '.TestMType',
'Version': {
'_type': 'test__version',
'Class': '.key.TestVersion',
}
}
},
{
'_type': 'element',
'Class': '.key.PElement',
'Id': {
'_type': 'TestId',
'Class': '.key.PModel',
'RollId': '.key.11261'
},
'Data': '.ref.root.Elements.0.Data'
},
{
'_type': 'element',
'Class': '.key.PElement',
'Id': {
'_type': 'TestId',
'Class': '.key.PModel',
'RollId': '.key.7914'
},
'Data': '.ref.root.Elements.0.Data'
}
]
}
}
}";
这是代码
public class Program
{
static void Main(string[] args)
{
//it provide json
var testCont = thirdpartyapi();
var dataList = new List<TestResponse>();
foreach (var testData in testCont.Elements())
{
var data = new TestResponse();
data.col1 = Convert.ToInt32(testData.Id().RollId());
data.col2 = testData.Data().MType().ToString();
dataList.Add(data);
}
}
public class TestResponse
{
public int col1 { get; set; }
public string col2 { get; set; }
}
}
【问题讨论】:
-
这个 JSON 无效。
-
@Adriani6 .. 抱歉,刚刚添加了 json 字符串。
-
@Harshit 您发布的代码未显示您尝试过的内容。
thirdpartyapi和Elements是什么?您使用的是特定的反序列化器还是关于查询 JSON 数据的问题?