【问题标题】:Comparing files not working as intended比较文件未按预期工作
【发布时间】:2018-07-03 14:49:07
【问题描述】:

大家好,有人可以向我解释为什么这不起作用。

我基本上必须拥有名为 Books 和 NewBooks 的文本文件...

文本文件从网络请求中填充,然后信息被解析到文本文件中......当我启动程序时,书籍和新书籍是相同的,并且几乎是彼此的副本。

完成了更多网络请求以更新 NewBooks 文本文件,当我比较它们时,如果 NewBooks 中有一行不在 Books 中,它会将该行添加到名为 myNewBooks 的第三个文本文件中。现在我将在这里展示的初始代码可以按预期工作

Dim InitialBooks = File.ReadAllLines("Books.json")
    Dim TW As System.IO.TextWriter
    'Create a Text file and load it into the TextWriter 
    TW = System.IO.File.CreateText("myNewBooks.JSON")

    Dim NewBooks = String.Empty
    Using reader = New StreamReader("NewBooks.json")
        Do Until reader.EndOfStream
            Dim current = reader.ReadLine
            If Not InitialBooks.Contains(current) Then 
                NewBooks = current & Environment.NewLine

                TW.WriteLine(NewBooks)
                TW.Flush()
                'Close the File 
            End If
        Loop
    End Using
    TW.Close() : TW.Dispose()

但是因为我的文本文件行中的部分字符串包含一个 url,有时我会发现同一本书具有不同的 url...我得到了重复的书籍条目,因为 url 是唯一的区别。所以我想我会在 url 之前拆分字符串,以便我只比较标题和描述以及区域...仅供参考,我的文本文件中的一行看起来类似于:

{ "Title": "My Title Here", "Description": "My Description Here", "Region": "My Region Here", "Url": "My Url Here", "Image": "My图片在这里”};

所以今天有个小伙伴帮我弄清楚了如何拆分我的线路,让它看起来更像这样:

{ "Title": "My Title Here", "Description": "My Description Here", "Region": "My Region Here", "Url"

这很好,但是现在当我比较它时,没有看到第一行包含分割线,我不明白为什么......这是修改后的代码。

Dim InitialBooks = File.ReadAllLines("Books.json")
    Dim TW As System.IO.TextWriter
    'Create a Text file and load it into the TextWriter 
    TW = System.IO.File.CreateText("myNewBooks.JSON")

    Dim NewBooks = String.Empty
    Using reader = New StreamReader("NewBooks.json")
        Do Until reader.EndOfStream
            Dim current = reader.ReadLine
            Dim splitAt As String = """Url"""
            Dim index As Integer = current.IndexOf(splitAt)
            Dim output As String = current.Substring(0, index + splitAt.Length)
            If Not InitialBooks.Contains(output) Then 
                NewBooks = current & Environment.NewLine

                TW.WriteLine(NewBooks)
                TW.Flush()
                'Close the File 
            End If
        Loop
    End Using
    TW.Close() : TW.Dispose()

您的智慧将不胜感激!

【问题讨论】:

    标签: vb.net file-handling


    【解决方案1】:

    您的 OP 令人困惑。

    如果我理解正确:

    您有 3 个文件 Books、NewBooks 和 MyBooks。 您从 Web 下载数据,如果该数据不在 Books 中,则将其添加到 NewBooks,否则添加到 MyBooks(重复)。

    看到您正在使用 JSON,我会按照以下方式进行操作。

    加载书籍,下载数据时检查它并与书籍进行比较。然后写入正确的文件。

    Imports System.Web.Script.Serialization ' for reading of JSON (+add the reference to System.Web.Extensions library)
    Dim JSONBooks = New JavaScriptSerializer().DeserializeObject(Books_string)
    

    使用断点检查 JSONBook。你会看到它的样子。 下载数据时,您可以简单地通过标题、网址或任何您想要的方式检查其中是否存在书籍。

    因为你只展示了一本书

    Debug.Print(JSONBooks("Title")) 'returns >>>My Title Here
    

    当你有更多时

    JSONBooks(x)("Title") 'where x is book number. 
    

    这样您就可以遍历所有书籍并检查您需要什么。

    JSON 数组长这样(如果需要构造的话)

    [{book1},{book2},...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 2019-11-27
      相关资源
      最近更新 更多