如何解析JSON字符串并检查结果是否包含文本,然后忽略或允许它?

时间:2014-12-13 15:11:01

标签: json string vb.net serialization jsonresult

我正在使用Minecraft启动器,我遇到了问题,因为我的启动器使用原始方式,例如,这里是JSON:

{
  "id": "1.8.1",
  "time": "2014-11-24T14:13:31+00:00",
  "releaseTime": "2014-11-24T14:13:31+00:00",
  "type": "release",
  "minecraftArguments": "--username ${auth_player_name} --version ${version_name} --gameDir ${game_directory} --assetsDir ${assets_root} --assetIndex ${assets_index_name} --uuid ${auth_uuid} --accessToken ${auth_access_token} --userProperties ${user_properties} --userType ${user_type}",
  "minimumLauncherVersion": 14,
  "assets": "1.8",
  "libraries": [
    {
      "name": "com.ibm.icu:icu4j-core-mojang:51.2"
    },
    {
      "name": "net.sf.jopt-simple:jopt-simple:4.6"
    },
    {
      "name": "com.paulscode:codecjorbis:20101023"
    },
    {
      "name": "com.paulscode:codecwav:20101023"
    },
    {
      "name": "com.paulscode:libraryjavasound:20101123"
    },
    {
      "name": "com.paulscode:librarylwjglopenal:20100824"
    },
    {
      "name": "com.paulscode:soundsystem:20120107"
    },
    {
      "name": "io.netty:netty-all:4.0.23.Final"
    },
    {
      "name": "com.google.guava:guava:17.0"
    },
    {
      "name": "org.apache.commons:commons-lang3:3.3.2"
    },
    {
      "name": "commons-io:commons-io:2.4"
    },
    {
      "name": "commons-codec:commons-codec:1.9"
    },
    {
      "name": "net.java.jinput:jinput:2.0.5"
    },
    {
      "name": "net.java.jutils:jutils:1.0.0"
    },
    {
      "name": "com.google.code.gson:gson:2.2.4"
    },
    {
      "name": "com.mojang:authlib:1.5.17"
    },
    {
      "name": "com.mojang:realms:1.7.5"
    },
    {
      "name": "org.apache.commons:commons-compress:1.8.1"
    },
    {
      "name": "org.apache.httpcomponents:httpclient:4.3.3"
    },
    {
      "name": "commons-logging:commons-logging:1.1.3"
    },
    {
      "name": "org.apache.httpcomponents:httpcore:4.3.2"
    },
    {
      "name": "org.apache.logging.log4j:log4j-api:2.0-beta9"
    },
    {
      "name": "org.apache.logging.log4j:log4j-core:2.0-beta9"
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl:2.9.1",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl_util:2.9.1",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl-platform:2.9.1",
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      },
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "net.java.jinput:jinput-platform:2.0.5",
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    },
    {
      "name": "tv.twitch:twitch:6.5"
    },
    {
      "name": "tv.twitch:twitch-platform:6.5",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "linux"
          }
        }
      ],
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows-${arch}",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    },
    {
      "name": "tv.twitch:twitch-external-platform:4.5",
      "rules": [
        {
          "action": "allow",
          "os": {
            "name": "windows"
          }
        }
      ],
      "natives": {
        "windows": "natives-windows-${arch}"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    }
  ],
  "mainClass": "net.minecraft.client.main.Main"
}

因此,在"libraries" ["name":及以下列出的游戏库中,库包含适用于它们的规则,我想知道这些规则与库文件名,以及使用它来应用操作,但我不知道,我使用此代码,但这只是重定向库。

这是我的代码:

 Dim item As String = Version
        Dim client As New WebClient()
        Await client.DownloadFileTaskAsync(New Uri("https://s3.amazonaws.com/Minecraft.Download/versions/" + item + "/" + item + ".json"), Root + "\versions\" + item + "\" + item + ".json")
        Dim JSONREADER As New StreamReader(Root + "\versions\" + item + "\" + item + ".json")
        json = JSONREADER.ReadToEnd()
        JSONREADER.Close()
        Dim JSONResult As Object = JsonConvert.DeserializeObject(Of Object)(json)
        MinecraftArgs = JSONResult("minecraftArguments")
        AssetIndex = JSONResult("assets")
        MainClass = JSONResult("mainClass")
        For Each i In JSONResult("libraries").Children()
            LibrariesList.Add(i.ToObject(Of MClibraries).name)
        Next

例如:

{
      "name": "org.lwjgl.lwjgl:lwjgl:2.9.1",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },

"名称"作为文件的名称,"规则"有两个动作:允许,禁止,在禁止下,有" os":,这设置动作,因为

"os": {
                "name": "osx"
              }

这只允许Windows和Linux中的文件,但不允许mac OS X中的文件,因此我想获取并应用此操作。

我想添加这些库:

"libraries": [
    {
      "name": "com.ibm.icu:icu4j-core-mojang:51.2"
    },
    {
      "name": "net.sf.jopt-simple:jopt-simple:4.6"
    },
    {
      "name": "com.paulscode:codecjorbis:20101023"
    },
    {
      "name": "com.paulscode:codecwav:20101023"
    },
    {
      "name": "com.paulscode:libraryjavasound:20101123"
    },
    {
      "name": "com.paulscode:librarylwjglopenal:20100824"
    },
    {
      "name": "com.paulscode:soundsystem:20120107"
    },
    {
      "name": "io.netty:netty-all:4.0.23.Final"
    },
    {
      "name": "com.google.guava:guava:17.0"
    },
    {
      "name": "org.apache.commons:commons-lang3:3.3.2"
    },
    {
      "name": "commons-io:commons-io:2.4"
    },
    {
      "name": "commons-codec:commons-codec:1.9"
    },
    {
      "name": "net.java.jinput:jinput:2.0.5"
    },
    {
      "name": "net.java.jutils:jutils:1.0.0"
    },
    {
      "name": "com.google.code.gson:gson:2.2.4"
    },
    {
      "name": "com.mojang:authlib:1.5.17"
    },
    {
      "name": "com.mojang:realms:1.7.5"
    },
    {
      "name": "org.apache.commons:commons-compress:1.8.1"
    },
    {
      "name": "org.apache.httpcomponents:httpclient:4.3.3"
    },
    {
      "name": "commons-logging:commons-logging:1.1.3"
    },
    {
      "name": "org.apache.httpcomponents:httpcore:4.3.2"
    },
    {
      "name": "org.apache.logging.log4j:log4j-api:2.0-beta9"
    },
    {
      "name": "org.apache.logging.log4j:log4j-core:2.0-beta9"
    },

如果规则允许Windows操作系统,请添加这些库:

{
      "name": "org.lwjgl.lwjgl:lwjgl:2.9.1",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl_util:2.9.1",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "org.lwjgl.lwjgl:lwjgl-platform:2.9.1",
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      },
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "osx"
          }
        }
      ]
    },
    {
      "name": "net.java.jinput:jinput-platform:2.0.5",
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    },
    {
      "name": "tv.twitch:twitch:6.5"
    },
    {
      "name": "tv.twitch:twitch-platform:6.5",
      "rules": [
        {
          "action": "allow"
        },
        {
          "action": "disallow",
          "os": {
            "name": "linux"
          }
        }
      ],
      "natives": {
        "linux": "natives-linux",
        "windows": "natives-windows-${arch}",
        "osx": "natives-osx"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    },
    {
      "name": "tv.twitch:twitch-external-platform:4.5",
      "rules": [
        {
          "action": "allow",
          "os": {
            "name": "windows"
          }
        }
      ],
      "natives": {
        "windows": "natives-windows-${arch}"
      },
      "extract": {
        "exclude": [
          "META-INF/"
        ]
      }
    }
  ],

如果在规则中仅禁止,并且仅在例如OS X中禁用,则表示Windows,并允许Linux。

1 个答案:

答案 0 :(得分:1)

首先编辑您的MClibraries类,使其如下所示:

Public Class MClibraries
    Public name As String
    Public rules As JArray
End Class

所以要做你要问的事情,我先从列表和字典开始:

Private ReadOnly _librariesList As New List(Of String)
Private ReadOnly _disallowed As New Dictionary(Of String, String)

注意:您已经拥有_librariesList,但您的名称为LibrariesList。

接下来,为了获得这些库,我做到了这一点:

Dim item As String = Version
Dim client = New WebClient() With {.Proxy = Nothing}
Dim json = Await client.DownloadStringTaskAsync(New Uri(String.Format("http://s3.amazonaws.com/Minecraft.Download/versions/{0}/{0}.json", item)))
Dim jsonResult As Object = JsonConvert.DeserializeObject(Of Object)(json)
For Each i In jsonResult("libraries")
    Dim entry As MClibraries = i.ToObject(Of MClibraries)() ' Converts object to MClibrary
    _librariesList.Add(entry.name) ' Add the name of each entry to the listbox
    If Not IsNothing(entry.rules) AndAlso entry.rules.Count = 2 Then ' Check to make sure it isn't empty
        _disallowed.Add(entry.rules(1)("os")("name")) ' Gets the os that is disallowed
    End If
Next

注意:只需添加已有的内容

即可

现在,您可以使用新词典" _disallowed"访问所有不允许使用的操作系统。关键是不允许的操作系统,值是包。

如果您想查看操作系统是否与用户操作系统匹配,您可以编写以下内容。此外,您还需要添加对System.Management的引用并将其导入

Private _currentOs as String 

'The following should go under the loading of a form
Dim osname As String = (From x In New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType(Of ManagementObject)()
            Select x.GetPropertyValue("Caption")).FirstOrDefault()

'The following should go where you retrieve the libraries
For Each i In jsonResult("libraries")
    Dim entry As MClibraries = i.ToObject(Of MClibraries)() ' Converts object to MClibrary
    If Not IsNothing(entry.rules) AndAlso entry.rules.Count = 2 Then ' Check to make sure it isn't empty
        If Not _currentOs.ToLower.Contains(entry.rules(1)("os")("name")) Then
            _librariesList.Add(entry.name)
        End If
    End If
Next