C# 使用Newtonsoft.Json迭代多个记录

C# 使用Newtonsoft.Json迭代多个记录,c#,vb.net,json.net,C#,Vb.net,Json.net,我正在C#或VB.net中寻找答案 我得到的字符串如下: {"1":{"pump":1,"name":"Pump 1","type":"VS","time":"2:10 PM","run":10,"mode":0,"drivestate":0,"watts":1656,"rpm":2850,"gpm":0,"ppc":0,"err":0,"timer":1,"duration":"durationnotset","currentrunning":{"mode":"off","value":0,"

我正在C#或VB.net中寻找答案

我得到的字符串如下:

{"1":{"pump":1,"name":"Pump 1","type":"VS","time":"2:10 PM","run":10,"mode":0,"drivestate":0,"watts":1656,"rpm":2850,"gpm":0,"ppc":0,"err":0,"timer":1,"duration":"durationnotset","currentrunning":{"mode":"off","value":0,"remainingduration":-1},"externalProgram":{"1":-1,"2":-1,"3":-1,"4":-1},"remotecontrol":1,"power":1,"friendlyName":"Pump 1"},"2":{"pump":2,"name":"Pump 2","type":"None","time":"timenotset","run":"runnotset","mode":"modenotset","drivestate":"drivestatenotset","watts":"wattsnotset","rpm":"rpmnotset","gpm":"gpmnotset","ppc":"ppcnotset","err":"errnotset","timer":"timernotset","duration":"durationnotset","currentrunning":{"mode":"off","value":0,"remainingduration":-1},"externalProgram":{"1":-1,"2":-1,"3":-1,"4":-1},"remotecontrol":"remotecontrolnotset","power":"powernotset","friendlyName":"Pump 2"}}
所以,只有两张唱片。我只需要把“名称”,“瓦特”和“转速”从每一个。我不需要将整个记录存储在数组或列表中,因为我只需要处理行


我怎样才能做到这一点呢?

您可以使用Newtonsoft.Json来做到这一点,只需在Nuget上找到它即可

你可以这样用,虽然有点脏

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = File.ReadAllText("TextFile1.txt");
            JObject jsonObject= (JsonConvert.DeserializeObject(json) as JObject);
            string name = jsonObject.Value<JObject>("1").Value<string>("name");
        }
    }
}
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用System.IO;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串json=File.ReadAllText(“TextFile1.txt”);
JObject jsonObject=(JsonConvert.DeserializeObject(json)作为JObject);
字符串名称=jsonObject.Value(“1”).Value(“名称”);
}
}
}
我用TextFile1中的json字符串测试了它,效果很好。您最好创建一个自定义类型,以便将其反序列化为

关键部分是
JsonConvert.DeserializeObject(json)
,它将json字符串转换为CLR对象,即
JObject
。然后是
.Value(“1”)
位,它只是使用newtonsoft api


p、 我注意到你在那里有一个json.net标签,他们的网站上几乎写着:

我感谢你的回答。虽然我至少能够反序列化一个响应,但我算不上是个程序员。我只是被困在其余的地方。