使用foreach循环读取和显示JSON文件

时间:2019-04-15 19:30:04

标签: c# json json.net

尝试使用foreach循环语句遍历JSON文件,但似乎收到以下错误 CS1579 foreach语句无法对类型为“ ReadJson”的变量进行操作,因为“ ReadJson”不包含“ GetEnumerator”的公共实例定义

在此处获取代码: https://dotnetfiddle.net/y1Q1Cn

using System;
using System.IO;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;


public partial class ReadJson
{
    [JsonProperty("items")]
    public Item[] Items { get; set; }
}

public partial class Item
{
    [JsonProperty("url")]
    public Uri Url { get; set; }

    [JsonProperty("item_xpath", NullValueHandling = NullValueHandling.Ignore)]
    public string ItemXpath { get; set; }

    [JsonProperty("item_size")]
    public string ItemSize { get; set; }
}

public partial class ReadJson
{
    public static ReadJson FromJson(string json) { return JsonConvert.DeserializeObject<ReadJson>(json, Converter.Settings); }
}

public static class Serialize
{
    public static string ToJson(this ReadJson self) { return JsonConvert.SerializeObject(self, Converter.Settings); }
}

internal static class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
        Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
    };
}

class UseJsonInVar
{
    public static void Test()
    {
        string filepath = "Question__55692935.json";

        File.WriteAllText(filepath, GetJson());

        StreamReader ddd = new StreamReader(filepath);
        var json = ddd.ReadToEnd();
        var objectJson = JsonConvert.DeserializeObject<ReadJson>(json);

        foreach (var item in objectJson)
        {
            var url = objectJson.Items[0].Url;
            var user = objectJson.Items[0].ItemXpath;
            var pass = objectJson.Items[0].ItemSize;
            Console.WriteLine("{0}\n {1}\n {2}", url, user, pass);
        }




    }

    static string GetJson()
    {
        var json = @"{
""items"":[
{
""url"":""https://www.nordstromrack.com/shop/Women/Clothing/Activewear/Jackets%20&%20Hoodies"",
""item_xpath"":""//*[@href='/shop/product/2299794/nike-vintage-drawstring-hoodie?color=BLACK%2FSAIL']"",
""item_size"":""//*[@href='?color=TRUBER%2FSAIL&size=L']""
},
{
""url"":""https://www.nordstromrack.com/events/281375/products/2584102/j-crew-cotton-cardigan?color=BLACK"",
""item_xpath"":""//*[@href='/events/281375/products/2584102/j-crew-cotton-cardigan?color=BLACK']"",
""item_size"":""//*[@href='?color=BLACK&size=M']""
}
]
}
";
        return json;
    }
}

public class Program
{
    public static void Main()
    {

        try
        {
            UseJsonInVar.Test();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Failed with unhandled exception: ");
            Console.WriteLine(ex);
            throw;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您无法在ReadJson中枚举foreach,因为它没有实现GetEnumerator方法。

我认为您想做的是枚举ReadJson的{​​{1}}成员,就像这样:

Items