首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >.NET处理JSON Feed

.NET处理JSON Feed
EN

Stack Overflow用户
提问于 2010-10-30 23:27:34
回答 3查看 1.9K关注 0票数 0

我正在寻找一个简单的示例,该示例从url调用提要,然后循环遍历数据,提取C#中的值。

我设法将提要数据放入一个字符串变量中,如下所示。我查看了newtonsoft.Json动态链接库,但找不到提取数据的简单示例。数据并不复杂,我已将其添加到底部。

所以基本上_feedData现在包含了我的JSON数据,我喜欢把它转换成一个JSON对象,然后把它的值提取出来。

代码语言:javascript
运行
复制
        static void Main(string[] args)
    {

        string _feedData = GetJSONFeed();


    }
     public static string GetJSONFeed()
    {
        string formattedUri = "http://www.myJsonFeed.com/blah.json";

        HttpWebRequest webRequest = GetWebRequest(formattedUri);
        HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
        string jsonResponse = string.Empty;
        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            jsonResponse = sr.ReadToEnd();
        }
        return jsonResponse;
    }

    private static HttpWebRequest GetWebRequest(string formattedUri)
    {
        // Create the request’s URI.
        Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);

        // Return the HttpWebRequest.
        return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
    }

我的JSON数据是这样的:

代码语言:javascript
运行
复制
[
 {
    "id": "9448",
    "title": "title title title",
    "fulltext": "main body text",
   "url": "http://www.flikr.co.uk?id=23432"
 },
 {
    "id": "9448",
    "title": "title title title",
    "fulltext": "main body text",
   "url": "http://www.flikr.co.uk?id=23432"
 }
]

谢谢你的帮助。抢夺

EN

回答 3

Stack Overflow用户

发布于 2010-10-31 00:01:35

遵循json-net项目:

如果您只对从JSON获取值感兴趣,没有要序列化或反序列化的类,或者JSON与您的类完全不同,而您需要手动读取和写入对象,则应该使用LINQ to

。LINQ to JSON允许您轻松地在.NET中读取、创建和修改JSON。

以及LINQ to JSON示例:

代码语言:javascript
运行
复制
string json = @"{
  ""Name"": ""Apple"",
  ""Expiry"": new Date(1230422400000),
  ""Price"": 3.99,
  ""Sizes"": [
    ""Small"",
    ""Medium"",
    ""Large"
  ]
}";

JObject o = JObject.Parse(json);
string name = (string)o["Name"];
// Apple
JArray sizes = (JArray)o["Sizes"];
string smallest = (string)sizes[0];
// Small
票数 2
EN

Stack Overflow用户

发布于 2010-10-30 23:37:28

创建一个类来保存提要数据的一个实例。

代码语言:javascript
运行
复制
public class FeedData
{
    public string id { get; set; }
    public string title { get; set; }
    public string fulltext { get; set; }
    public string url { get; set; }
}

然后,在获得字符串形式的json响应后,将其反序列化为一个FeedData对象列表。

代码语言:javascript
运行
复制
var serializer = new DataContractJsonSerializer(typeof(List<FeedData>));
using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(jsonResponse)))
{
   var serializer = new DataContractJsonSerializer(typeof(List<FeedData>));
   return serializer.ReadObject(ms) as List<FeedData>;
}

请注意,返回值现在应为List<FeedData>IEnumerable<FeedData>

票数 0
EN

Stack Overflow用户

发布于 2010-10-30 23:43:13

我猜您需要将JSON字符串解析为CLR对象,而不是JSON对象,对吧?

这个documentation页面提到了这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4059342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档