2015-01-08 30 views
1

我从web服务中撤销json并使用$ .each()遍历层次结构。我遇到的问题是“标题”,可能会出现单个标题或多个标题。如何强制“标题”返回一个对象数组,即使集合中只有一个对象,所以$ .each()不会爆炸。在the documentation从webservice返回单个json对象的数组

'VB Webservice... 
ds.DataSetName = "dsSurveys" 
ds.Tables(0).TableName = "Surveys" 
ds.Tables(1).TableName = "Titles" 
ds.Tables(2).TableName = "Questions" 
ds.Tables(3).TableName = "ResponseTypes" 

Dim relSurveyTitles As New DataRelation("relSurveyTitles", ds.Tables("Surveys").Columns("SurveyId"), ds.Tables("Titles").Columns("SurveyId")) 
Dim relTitlesQuestions As New DataRelation("relTitlesQuestions", ds.Tables("Titles").Columns("TitleId"), ds.Tables("Questions").Columns("TitleId")) 
Dim relResponseTypesQuestions As New DataRelation("relResponseTypesQuestions", ds.Tables("ResponseTypes").Columns("ResponseTypeId"), ds.Tables("Questions").Columns("ResponseTypeId")) 

relSurveyTitles.Nested = True 
relTitlesQuestions.Nested = True 
relResponseTypesQuestions.Nested = False 

ds.Relations.Add(relSurveyTitles) 
ds.Relations.Add(relTitlesQuestions) 
ds.Relations.Add(relResponseTypesQuestions) 

Dim doc As New XmlDocument() 
doc.LoadXml(ds.GetXml()) 
Dim jsonText As String = JsonConvert.SerializeXmlNode(doc).Replace("null", """""").Replace("'", "'") 
Return jsonText 
//json response 
{ 
    "dsSurveys": { 
     "Surveys": { 
      "SurveyId": "1", 
      "SurveyName": "Survey 1", 
      "Titles": {  //I would like to see this in an array (like "Questions") --> "Titles": [{ 
          //regarless of object count 
       "SurveyId": "1", 
       "TitleId": "1", 
       "TitleName": "Title 1", 
       "Questions": [{ 
        "SurveyId": "1", 
        "TitleId": "1", 
        "QuestionId": "1", 
        "Question": "Question 1?", 
        "isComment": "true", 
        "ResponseTypeId": "1" 
       }, 
       { 
        "SurveyId": "1", 
        "TitleId": "1", 
        "QuestionId": "2", 
        "Question": "Question 2?", 
        "isComment": "true", 
        "ResponseTypeId": "1" 
       }] 
      } 
     }, 
     "ResponseTypes": { 
      "ResponseTypeId": "1", 
      "ResponseType": "Yes|No|N/A" 
     } 
    } 
} 
+0

运行'Object.keys':

您可以添加使用像这样适当的属性。这将返回'title'对象上存在的一组属性键。您可以使用'titles [key]'来访问对象属性。 – Mouser

回答

1

寻找,似乎可以强制Titles序列化作为数组,即使只有一个发生,通过添加json:Array='true'属性(其中JSON命名空间是http://james.newtonking.com/projects/json)到<Titles>节点。在`titles`

Dim doc As New XmlDocument() 
doc.LoadXml(ds.GetXml()) 

' Add the namespace declaration to the root node 
Dim nsAttr = doc.CreateAttribute("xmlns", "json", "http://www.w3.org/2000/xmlns/") 
nsAttr.Value = "http://james.newtonking.com/projects/json" 
doc.DocumentElement.Attributes.Append(nsAttr) 

' Add the json:Array attribute to each Titles element 
For Each title As XmlElement In doc.SelectNodes("//Titles") 
    Dim attr = doc.CreateAttribute("json:Array", "http://james.newtonking.com/projects/json") 
    attr.Value = "true" 
    title.Attributes.Append(attr) 
Next 

Dim jsonText As String = JsonConvert.SerializeXmlNode(doc).Replace("null", """""").Replace("'", "'") 
Return jsonText 
+0

谢谢!正是我在找什么。 – aas4mis