如何从C#中的json获取数据?

时间:2019-01-18 11:07:08

标签: c# json

我以json格式从第三方获取json数据。  在某些情况下,我试图从“ Id”中获取<script> setTimeout(function(){ var hash = window.location.hash.substr(1); if (hash == "disqus_thread") { location.hash = "#comments"; location.hash = "#" + hash; }; }, 1000); </script> ,从“ Data”中获取RollId “数据”没有字段,它是空白。

当我们有“数据”时,它正在工作。如果为空白,则不起作用。 有什么想法吗?有更好的方法吗?

这是杰森

MType

这是代码

      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'
        }

      ]
    }
  }
}";

3 个答案:

答案 0 :(得分:0)

首先,尝试获取有效的格式正确的json。您可以使用此code beautify tool。然后,您可以使用json2csharp自动生成C#类。最后,有了C#类,您可以应用if语句并检查property是否为空。

生成的C#包装器:

public class Id
{
    public string _type { get; set; }
    public string Class { get; set; }
    public string RollId { get; set; }
}

public class Datum
{
    public string _type { get; set; }
    public string Class { get; set; }
    public Id Id { get; set; }
    public object Data { get; set; }
}

public class Elements
{
    public string _type { get; set; }
    public List<Datum> _data { get; set; }
}

public class Root
{
    public string _type { get; set; }
    public string Class { get; set; }
    public Elements Elements { get; set; }
}

public class RootObject
{
    public int encoding_version { get; set; }
    public Root root { get; set; }
}

答案 1 :(得分:0)

如果您只想从复杂的JSON中选择少数字段,则可以考虑使用Cinchoo ETL-一个开源库

下面的示例显示了如何从json中选择RollIdMType

using (var r = new ChoJSONReader("## YOUR JSON FILE PATH ##")
    .WithJSONPath("$.._data")
    .WithField("RollId", jsonPath: "$..Id.RollId", fieldType: typeof(string))
    .WithField("MType", jsonPath: "$..Data.MType", fieldType: typeof(string))
    )

{
    foreach (var rec in r)
    {
        Console.WriteLine((string)rec.RollId);
        Console.WriteLine((string)rec.MType);
    }
}

希望有帮助。

答案 2 :(得分:-1)

最简单的方法是使用DataContractJsonSerializer

Here,您可以阅读更多有关它的内容。

总共需要创建一个模型,该模型具有与json相同的可能结果。 然后,您可以使用DataContractJsonSerializer来使用MemoryStream创建模型对象。

Here,您可以找到一个不错的工具,可以从JSON创建模型。 例如:

public class Id
{
    public string _type { get; set; }
    public string Class { get; set; }
    public string RollId { get; set; }
}

public class Datum
{
    public string _type { get; set; }
    public string Class { get; set; }
    public Id Id { get; set; }
    public object Data { get; set; }
}

public class Elements
{
    public string _type { get; set; }
    public List<Datum> _data { get; set; }
}

public class Root
{
    public string _type { get; set; }
    public string Class { get; set; }
    public Elements Elements { get; set; }
}

public class RootObject
{
    public int encoding_version { get; set; }
    public Root root { get; set; }
}

然后,您使用MemoryStreamDataContractJsonSerializer从该JSON创建RootObject的对象。

MemoryStream stream1 = new MemoryStream();  
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));  

stream1.Position = 0;  
RootObject rootObject = (RootObject)ser.ReadObject(stream1);

是的,正如Adriani6所提到的-您的JSON目前无效:

"Data": {
            "_type": "p_model",
            "Class": ".key.Unsupported",
            "MType": ".TestMType",
            "Version": {
              "_type": "test__version",
              "Class": ".key.TestVersion",

            }

不允许在末尾使用,