如何从C#中的json对象获取值

时间:2019-07-17 09:16:41

标签: c# json

能否请您告诉我如何从Json对象获取值。我不知道是否需要将其转换为类,还是可以直接从.json文本文件中获取它。这是我创建的json文件:

{
  "801": {
    "Name": "Tarlac",
    "Lanes": {
      "2": {
        "Ip": "172.23.101.21"
      },
      "4": {
        "Ip": "172.23.101.41"
      },
      "6": {
        "Ip": "172.23.101.61"
      },
      "8": {
        "Ip": "172.23.101.81"
      },
      "9": {
        "Ip": "172.23.101.91"
      },
      "11": {
        "Ip": "172.23.101.111"
      }
    }
  },
  "803": {
    "Name": "Victoria",
    "Lanes": {
      "3": {
        "Ip": "172.23.103.31"
      },
      "6": {
        "Ip": "172.23.103.61"
      }
    }
  },
  "805": {
    "Name": "Pura",
    "Lanes": {
      "4": {
        "Ip": "172.23.105.41"
      },
      "6": {
        "Ip": "172.23.105.61"
      },
      "9": {
        "Ip": "172.23.105.91"
      },
      "7": {
        "Ip": "172.23.105.71"
      }
    }
  },
  "807": {
    "Name": "Ramos",
    "Lanes": {
      "3": {
        "Ip": "172.23.107.31"
      },
      "5": {
        "Ip": "172.23.107.51"
      }
    }
  },
  "809": {
    "Name": "Anao",
    "Lanes": {
      "3": {
        "Ip": "172.23.109.31"
      },
      "5": {
        "Ip": "172.23.109.51"
      }
    }
  },
  "811": {
    "Name": "Carmen",
    "Lanes": {
      "2": {
        "Ip": "172.23.111.21"
      },
      "4": {
        "Ip": "172.23.111.41"
      },
      "6": {
        "Ip": "172.23.111.61"
      }
    }
  },
  "813": {
    "Name": "Urdaneta",
    "Lanes": {
      "4": {
        "Ip": "172.23.113.41"
      },
      "6": {
        "Ip": "172.23.113.61"
      },
      "8": {
        "Ip": "172.23.113.81"
      },
      "9": {
        "Ip": "172.23.113.91"
      }
    }
  },
  "815": {
    "Name": "Binalonan",
    "Lanes": {
      "3": {
        "Ip": "172.23.115.31"
      },
      "5": {
        "Ip": "172.23.115.51"
      }
    }
  },
  "817": {
    "Name": "Pozorrubio",
    "Lanes": {
      "3": {
        "Ip": "172.23.117.31"
      },
      "4": {
        "Ip": "172.23.117.41"
      },
      "6": {
        "Ip": "172.23.117.61"
      }
    }
  }
}

我尝试创建一个类,以便它可以保存我的所有json值,并且“也许”可以通过它进行搜索,如下所示:

using System.Collections.Generic;

namespace TagReporting.Models
{
    class Plaza
    {
        public string Code { get; set; }
        public PlazaInfo PlazaInfo { get; set; }
    }

    class PlazaInfo
    {
        public string Name { get; set; }
        public List<Lane> Lanes { get; set; }
    }

    class Lane
    {
        public string Code { get; set; }
        public string IpAddress { get; set; }
    }
}

并尝试使用如下代码对其进行反序列化:

private void GetPlazaInformation()
{
    using (var streamReader = new StreamReader(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location) ?? throw new InvalidOperationException(), "Data/plaza.json")))
    {
        var json = streamReader.ReadToEnd();
        var plaza = JsonConvert.DeserializeObject<Plaza>(json);
    }
}

放置一个断点,我得到的只是一个空值,像这样:

  

代码:null,PlazaInfo:null

我只想在json文件中搜索一个值。示例:

获取“ 813”巷“ 6”的IP,即“ 172.23.113.61”

请帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

喜欢吗? Snippet on Fiddle

using System;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {
        String Json = "{'801': {'Name': 'Tarlac','Lanes': {'2': {'Ip': '172.23.101.21'},'4': {'Ip': '172.23.101.41'},'6': {'Ip': '172.23.101.61'},'8': {'Ip': '172.23.101.81'},'9': {'Ip': '172.23.101.91'},'11': {'Ip': '172.23.101.111'}}},'803': {'Name': 'Victoria','Lanes': {'3': {'Ip': '172.23.103.31'},'6': {'Ip': '172.23.103.61'}}},'805': {'Name': 'Pura','Lanes': {'4': {'Ip': '172.23.105.41'},'6': {'Ip': '172.23.105.61'},'9': {'Ip': '172.23.105.91'},'7': {'Ip': '172.23.105.71'}}},'807': {'Name': 'Ramos','Lanes': {'3': {'Ip': '172.23.107.31'},'5': {'Ip': '172.23.107.51'}}},'809': {'Name': 'Anao','Lanes': {'3': {'Ip': '172.23.109.31'},'5': {'Ip': '172.23.109.51'}}},'811': {'Name': 'Carmen','Lanes': {'2': {'Ip': '172.23.111.21'},'4': {'Ip': '172.23.111.41'},'6': {'Ip': '172.23.111.61'}}},'813': {'Name': 'Urdaneta','Lanes': {'4': {'Ip': '172.23.113.41'},'6': {'Ip': '172.23.113.61'},'8': {'Ip': '172.23.113.81'},'9': {'Ip': '172.23.113.91'}}},'815': {'Name': 'Binalonan','Lanes': {'3': {'Ip': '172.23.115.31'},'5': {'Ip': '172.23.115.51'}}},'817': {'Name': 'Pozorrubio','Lanes': {'3': {'Ip': '172.23.117.31'},'4': {'Ip': '172.23.117.41'},'6': {'Ip': '172.23.117.61'}}}}".Replace('\'','"');
        JObject JsonDe = JObject.Parse(Json);
        Console.WriteLine(JsonDe["813"]["Lanes"]["6"]);
    }
}