【问题标题】:Error read json string读取 json 字符串时出错
【发布时间】:2013-10-17 08:49:55
【问题描述】:

我正在尝试从 http 帖子中读取 json 字符串,我正在尝试通过手动填充 json 字符串来测试我的代码,但我的问题是我无法多次添加同一个节点。 ..换句话说,如果我输入以下内容,它会起作用:

    Dim json As String = "{'name': 'jocelyne','mo': '70274724'} 

但如果我输入以下内容,它将不起作用:

    Dim json As String = "{'name': 'jocelyne','mo': '70274724'},{'name': 'eliane','mo': '12345678'}"

我实际上有数百个姓名和 mo 号码,它们将以 json 字符串发送给我

这是我的全部代码:

    Request.InputStream.Position = 0 'you need this else the magic doesn't happen
    Dim inputStream As New StreamReader(Request.InputStream)
    'Dim json As String = inputStream.ReadToEnd()
    Dim json As String = "{'name': 'jocelyne','mo': '70274724'},{'name': 'eliane','mo': '12345678'}"

    Dim jss As New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim dict As Dictionary(Of String, String) = jss.Deserialize(Of Dictionary(Of String, String))(json)

    For Each item As KeyValuePair(Of String, String) In dict
        Response.Write(item.Key & " - " & item.Value & "<br>")
    Next

那么我该如何解决这个问题?我只需要能够读取使用 http post 给我的这种 json 格式并反序列化它以读取数据

【问题讨论】:

  • 您需要在 JSON 字符串中传递 JSON 对象数组,例如 - "[{}],[{}]"
  • @Vandesh 如果我传递数组,我会收到以下错误:Invalid JSON primitive: [{'name': 'eliane','mo': '12345678'}]. 初始化字典时

标签: json vb.net post http-post vb.net-2010


【解决方案1】:

需要将整个字符串放在括号中

Dim serializer As New Web.Script.Serialization.JavaScriptSerializer()
Dim json As String = "[{'name': 'jocelyne','mo': '70274724'},{'name': 'eliane','mo': '12345678'}]"
Dim tst = serializer.Deserialize(Of Object)(json)

结果是对象数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多