繁体   English   中英

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

[英]How to get a value from a json object in C#

你能告诉我如何从 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);
    }
}

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

代码:空,PlazaInfo:空

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

获取“813”车道“6”的IP,即“172.23.113.61”

我需要你的帮助。 谢谢你。

像这样 ? 小提琴上的片段

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"]);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM