json字符串中不止一个命中

时间:2015-04-08 12:43:19

标签: asp.net json vb.net

我创建了一个从服务中获取数据并将其添加到我的应用程序的函数。问题是当我得到超过1次"重复"时,一切都停止了。这意味着如果我得到以下字符串的结果,我就无法正确获取数据:

{
"qry":"Programming service",
"result":{
"hitLinesBeforeFilter":1,
"approxHits":1,
"userID":299228,
"1":{"listing":{
"table":"listing",
"id":"693144",
"duplicates":
[
{
"table":"listing",
"id":"693144:0",
"idlinje":"D1E6C4B001",
"tlfnr":"99886644",
"etternavn":"Programming Service AS",
"veinavn":"Kolåsveien",
"husnr":"11",
"postnr":"1234",
"virkkode":"N",
"apparattype":"M",
"telco":"MF",
"kilde":"D",
"foretaksnr":"123456789",
"bransjekode":"19940",
"prioritet":"0",
"kommunenr":"1234",
"poststed":"Bergen",
"kommune":"Bergen",
"fylke":"Hordaland",
"landsdel":"V",
"bransjebokmaal":"Programming and software",
"bransjenynorsk":"Programming and software"
},
**{
"table":"listing",
"id":"693144:1",
"idlinje":"D1E6C4B000",
"bransjekode":"46955",
"bransjebokmaal":"hardware",
"bransjenynorsk":"hardware"
}**
]
}
},
"dummy":null
}
}

我的问题是,无论何时在回报中添加最后一部分(在强/ **中),它都无法正常工作。

我的其余代码如下:

Class Result
        Property listing As Listing
    End Class

Class Listing
        Property table As String
        Property id As String
        Property idlinje As String
        Property duplicates As Duplicate()
    End Class

Class Duplicate
        Property table As String
        Property id As String
        Property idlinje As String
        Property tlfnr As String
        Property etternavn As String
        Property fornavn As String
        Property veinavn As String
        Property husnr As String
        Property postnr As String
        Property virkkode As String
        Property apparattype As String
        Property telco As String
        Property kilde As String
        Property bkdata As String
        Property prioritet As String
        Property fodselsdato As String
        Property kommunenr As String
        Property poststed As String
        Property kommune As String
        Property fylke As String
        Property landsdel As String
        Property foretaksnr As String
    End Class

Public Sub findContact()
        lblContactResults.Visible = True
        Dim contactLink As String
        Dim phone As String = newCustSearch.Text
        Dim url As String = "My string on web to get the json data"
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim json As String = reader.ReadToEnd
        Dim o As JObject = JObject.Parse(json)
        Dim i As Integer = 1
        Dim results = o("result")

        For Each resultProperty In results.Value(Of JObject)()
            'Only get properties like "1" inside the root "result" property
            If Not Integer.TryParse(resultProperty.Key, Nothing) Then Continue For
            'Approach 1: Manually Iterate over the duplicates array inside each result
            'Dim duplicatesArray = resultProperty.Value("listing")("duplicates").Value(Of JArray)()
            'For Each duplicate In duplicatesArray
            'Make sure there is a fornavn property
            'If Duplicate("fornavn") Is Nothing Then Continue For
            ' Console.WriteLine(Duplicate("fornavn"))
            'Next

            'Approach 2: Deserialize the listing into a .Net object
            Dim serializer As JsonSerializer = New JsonSerializer()
            Dim resultObject As Result = JsonConvert.DeserializeObject(Of Result)(resultProperty.Value.ToString())

            For Each duplicateObject In resultObject.listing.duplicates
                Dim firstName As String = ""
                Dim middleName As String = ""
                If duplicateObject.fornavn <> "" And duplicateObject.fornavn.Contains(" ") Then
                    Dim name As String() = duplicateObject.fornavn.Split(" ")
                    firstName = name(0)
                    middleName = name(1)
                Else
                    firstName = duplicateObject.fornavn
                End If

                contactLink = "<a href='#" + i.ToString + "'class='customerEniro' onClick='test()' data-contact-Firstname='" + firstName + "' data-contact-Middlename='" + middleName + "' data-contact-Lastname='" + duplicateObject.etternavn + "' data-contact-phone='" + duplicateObject.tlfnr + "'data-contact-born='" + Convert.ToDateTime(duplicateObject.fodselsdato).ToString("dd.MM.yyyy") + "' data-contact-address='" + duplicateObject.veinavn + " " + duplicateObject.husnr + "'data-contact-addresszip='" + duplicateObject.postnr + "'data-contact-addressplace='" + duplicateObject.poststed + "'data-contact-eniro='" + duplicateObject.idlinje + "' > "
                contactLink += i.ToString + " - " + duplicateObject.fornavn + " " + duplicateObject.etternavn + " - " + duplicateObject.tlfnr + " - " + duplicateObject.veinavn + " " + duplicateObject.husnr + " - " + duplicateObject.postnr + " " + duplicateObject.poststed
                contactLink += "</a>" + vbNewLine
                lblContactResults.Text += contactLink
                i += 1
            Next
        Next
        btnSaveNewCustomer.Visible = True

        If lblContactResults.Text = "" Then
            lblContactResults.Text = "No hits. Try again!"
            lblMsg.Text = "No one was found."
        End If
        updatePanel2.Update()
    End Sub

任何人都可以看到代码失败的任何明显原因吗?我认为有重复的东西和不止一次的打击,我无法处理。

1 个答案:

答案 0 :(得分:0)

我相信这一行:If duplicateObject.fornavn <> "" And duplicateObject.fornavn.Contains(" ") Then是罪魁祸首。多个副本很好。如果你看到json,fornavn不在那里,因此在反序列化期间,fornvavn将保持为null。在null原因问题上执行.Contain(...)

将其更改为字符串null检查并使用AndAlso将其链接以短路和运算符,如下所示:

If Not string.IsNullOrWhiteSpace(duplicateObject.fornavn) AndAlso duplicateObject.fornavn.Contains(" ") Then

工作示例:https://dotnetfiddle.net/e2DE1l