远程读取JSON数据

时间:2011-09-17 03:38:49

标签: c# asp.net json

更新1

我尝试实现,但当我将鼠标悬停在我的主题上时,我看到TopicId和TopicName为空,我看到myJSON字符串中的数据。

我还有什么需要做的?我错过了什么?

Topic topic = new Topic();
 MemoryStream stream1 = new MemoryStream(Encoding.Unicode.GetBytes(myJSON));
 //stream1.Position = 0;
 DataContractJsonSerializer serialize = new DataContractJsonSerializer(typeof(Topic));
 //topic = (Topic)serialize.ReadObject(stream1);
 Topic p2 = (Topic)serialize.ReadObject(stream1);
 stream1.Close();  //later i will use in `using statement`
 stream1.Dispose();

PS:我只有Topic类就足够了,还是我必须创建jcolebrand在下面显示的所有类?

我创建了一个名为Topic的类,其中有两个prop

[DataContract] 
    public class Topic 
    {       
        [DataMember]
        public string TopicId { get; set; }
        [DataMember]
        public string TopicName { get; set; } 
    } 

更新1结束

我正在处理一个返回JSON数据的需求,我需要一种方法来解析数据并将数据加载到下拉列表中,我正在寻找JSON中名为TopicName的元素

提取TopicName后,我将把这些数据加载到DropDownList asp.net控件中 (不使用JQuery或JavaScript)

这里是JSON数据:

[{"NS":{"Count":1},
  "Source":{"Acronym":"ABC","Name":"Name"},
  "Item":[{"Id":"12312",
           "Url":"http://sitename",
           "ContentItem":[{"NS":{"Count":1},
                           "SourceUrl":"sitename",
                           "ContentType":"text/xml",
                           "PersistentUrl":"sitename",
                           "Title":"MY TITLE",
                           "SelectionSpec":{"ClassList":"","ElementList":"","XPath":null},
                           "Language":{"Value":"eng","Scheme":"ISO 639-2"},
                           "Source":{"Acronym":"ABC","Name":"Name","Id":null},
                           "Topics":[{"Scheme":"ABC",
                                      "Topic":[{"TopicId":"6544","TopicName":"TOPIC NAME1"},
                                               {"TopicId":"63453","TopicName":"TOPIC NAME2"},
                                               {"TopicId":"2343","TopicName":"TOPIC NAME3"},
                                               {"TopicId":"2342","TopicName":"TOPIC NAME4"}]
                                    }],
                           "ContentBody":null
                        }]
         }]
 },

[{"NS":{"Count":1},"Source":{"Acronym":"ABC1","Name":"Name1"},"Item":[{"Id":"123121","Url":"http://sitename1","ContentItem":[{"NS":{"Count":1},"SourceUrl":"sitename","ContentType":"text/xml","PersistentUrl":"sitename1","Title":"MY TITLE1","SelectionSpec":{"ClassList":"","ElementList":"","XPath":null},"Language":{"Value":"eng","Scheme":"ISO 639-2"},
"Source":{"Acronym":"ABC1","Name":"Name1","Id":null},"Topics":[{"Scheme":"ABC1","Topic":[{"TopicId":"65441","TopicName":"TOPIC NAME11"},{"TopicId":"634531","TopicName":"TOPIC NAME21"},{"TopicId":"23431","TopicName":"TOPIC NAME31"},{"TopicId":"23421","TopicName":"TOPIC NAME41"}]}],"ContentBody":null}]}]},

2 个答案:

答案 0 :(得分:2)

假设上面应用的重新缩进是正确的,那么你有以下类(显然)

public class OuterWrapper {
  public NS NS { get; set; }
  public Source Source { get; set; }
  public ContentItemWrapper[] Item { get; set; } 
}
public class ContentItemWrapper {
  public int Id { get; set; }
  public string Url { get; set; }
  public ContentItem[] ContentItem { get; set; }
}
public class ContentItem {
  public NS NS { get; set; }
  public SourceUrl { get; set; }
  // I'm gonna skip a bunch of fields, you get the idea
  public Topics Topic { get; set; }
}
public class Topics {
  public string Scheme { get; set; }
  public Topic[] Topic { get; set; }
}
public class Topic {
  public string TopicId { get; set; }
  public string TopicName { get; set; }
}

你做的是你使用那组类型声明(特别是OuterWrapper)到DataContractJsonSerializer将JSON解码成一个C#对象,然后你可以使用强类型方法等来查询。这是C#做的那些时候之一没有任何接近Javascript的灵活性,因为必须明确声明所有内容。

答案 1 :(得分:0)

尝试为JSON使用内置序列化程序 - http://msdn.microsoft.com/en-us/library/bb412179.aspxnew DataContractJsonSerializer(typeof(Person)).ReadObject(stream1);

如果仅仅读取对象是不够的,请考虑使用JSON.Net(http://json.codeplex.com/) - JsonConvert.DeserializeObject<Labels>(json);