【问题标题】:Get JSON Column Names from ASMX Web Service从 ASMX Web 服务获取 JSON 列名
【发布时间】:2020-09-28 22:57:20
【问题描述】:

我正在从我的 WebMethod 接收此 JSON:

{
    "TableName": "myTable",
    "Table": [
    {
      "ProdOrder": "245392",
      "Item": "C01000FLS0300GF",
      "Qty": 40
    },
    {
      "ProdOrder": "245393",
      "Item": "C01000FLS0400GF",
      "Qty": 20
    }
  ]
}

这是网络方法:

<WebMethod()>
Public Function MyWebMethod(strJSON As String) As String
    Dim objJSON As Object = New JavaScriptSerializer().Deserialize(Of Object)(strJSON)
    'Get TableName
    Dim strTableName As String = objJSON("TableName")
    'Get Data
    Dim arrJSON As Object() = objJSON("Table")
    For i As Integer = 0 To arrJSON.Length - 1
        Dim ProdOrder As String = arrJSON(i)("ProdOrder")
        Dim Item As String = arrJSON(i)("Item")
        Dim Qty As String = arrJSON(i)("Qty")
    Next
    Return "OK"
End Function

直到这里一切都非常简单并且可以工作。

我现在的问题是,如果我不知道字段的名称,如何获取它们?

我的意思是,任何获取“ProdOrder”、“Item”和“Qty”的方式......

使用 arrJSON(i)("ProdOrder") 我得到了价值。如何获得标题“ProdOrder”?

【问题讨论】:

    标签: json vb.net asmx


    【解决方案1】:

    我找到了办法:

    Public Function MyWebMethod(strJSON As String) As String
        Dim objJSON As Object = New JavaScriptSerializer().Deserialize(Of Object)(strJSON)
        Dim strTableName, strFields, strValues As String
        For Each JSONItem As KeyValuePair(Of String, Object) In objJSON
            Select Case JSONItem.Key
                Case "TableName"
                    'Get TableName
                    strTableName = JSONItem.Value
                Case "Table"
                    'Get Data
                    For Each DataRecord As Object In JSONItem.Value
                        strFields = ""
                        strValues = ""
                        For Each DataField As KeyValuePair(Of String, Object) In DataRecord
                            strFields &= DataField.Key & ","
                            strValues &= DataField.Value & ","
                        Next
                    Next
            End Select
        Next
        Return "OK"
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 2016-12-02
      • 2020-07-27
      相关资源
      最近更新 更多