在C#中解析JSON查询 [英] Parse JSON query in C#

查看:97
本文介绍了在C#中解析JSON查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用C#开发一个网站,该网站解析JSON查询中的数据,以便以后使用.到目前为止,我已经找到了所有要使用的库,但是经过多次尝试后解析仍无法正常进行,请您提供帮助.我使用Visual Studio 2010 Professional.

I want to develop an website in C# that parses data from a JSON query in order to use them later. So far I have found all the libraries to use, but after many attempts parsing does not work and I would like your help please. I use visual studio 2010 professional.

这是C#代码.

using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Json;
using System.Text;
using Newtonsoft.Json.Linq;

namespace TwitterFeed
{
public partial class URLReader : System.Web.UI.Page
{
    static string username = null;
    static string result = null;
    static string url = null;
    JsonTextParser parser;
    JsonObject obj;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ButtonTest_Click(object sender, EventArgs e)
    {
        username = TextBoxUsername.Text;    // takes the username imported by the user
        if (username == "")
        {
            TextBoxTest.Text = "Please enter a username...";    // informs the user that no username was imported
        }
        else
        {
            url = "JSONQuery.txt";
            try
            {
                WebClient client = new WebClient();     // The methods needed taking the content of the URL
                result = client.DownloadString(url);    // Putting the JSON content of the URL into a string

                JObject obj = JObject.Parse(result);
                Console.WriteLine("name: " + obj["name"]);
            } 
            catch (Exception ex)
            {
                TextBoxTest.Text = ex.Message;
            }
        }
    }
}

}

这是在输出中显示的错误:

This is the error that is displayed in the output:

A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.DLL

这是JSON代码:

    {  
   "created_at":"Sun Apr 08 14:51:37 +0000 2012",
   "id":189002564061241344,
   "id_str":"189002564061241344",
   "text":"Another invention from google. http:\/\/t.co\/Fcx13JrJ #google",
   "source":"web",
   "truncated":false,
   "in_reply_to_status_id":null,
   "in_reply_to_status_id_str":null,
   "in_reply_to_user_id":null,
   "in_reply_to_user_id_str":null,
   "in_reply_to_screen_name":null,
   "user":{  
      "id":132681798,
      "id_str":"132681798",
      "name":"Charilaos Eristikos",
      "screen_name":"EristikosTigka",
      "location":"Glasgow, Scotland",
      "url":null,
      "description":"Technology Lives Between Us.",
      "protected":false,
      "followers_count":52,
      "friends_count":133,
      "listed_count":2,
      "created_at":"Tue Apr 13 22:28:19 +0000 2010",
      "favourites_count":1,
      "utc_offset":0,
      "time_zone":"Edinburgh",
      "geo_enabled":false,
      "verified":false,
      "statuses_count":64,
      "lang":"en",
      "contributors_enabled":false,
      "is_translator":false,
      "profile_background_color":"FFFFFF",
      "profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/209319404\/8.jpg",
      "profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/209319404\/8.jpg",
      "profile_background_tile":true,
      "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1632508237\/380352_2437786137920_1049741902_2747933_696669280_n_normal.jpg",
      "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1632508237\/380352_2437786137920_1049741902_2747933_696669280_n_normal.jpg",
      "profile_link_color":"00B093",
      "profile_sidebar_border_color":"9E9E9E",
      "profile_sidebar_fill_color":"DDEEF6",
      "profile_text_color":"000000",
      "profile_use_background_image":true,
      "show_all_inline_media":false,
      "default_profile":false,
      "default_profile_image":false,
      "following":null,
      "follow_request_sent":null,
      "notifications":null
   },
   "geo":null,
   "coordinates":null,
   "place":null,
   "contributors":null,
   "retweet_count":0,
   "entities":{  
      "hashtags":[  
         {  
            "text":"google",
            "indices":[  
               52,
               59
            ]
         }
      ],
      "urls":[  
         {  
            "url":"http:\/\/t.co\/Fcx13JrJ",
            "expanded_url":"http:\/\/phys.org\/news\/2012-04-google-floor-app-venue-owners.html",
            "display_url":"phys.org\/news\/2012-04-g\u2026",
            "indices":[  
               31,
               51
            ]
         }
      ],
      "user_mentions":[  

      ]
   },
   "favorited":false,
   "retweeted":false,
   "possibly_sensitive":false
},
{  
   "created_at":"Thu Apr 05 16:24:08 +0000 2012",
   "id":187938685436698624,
   "id_str":"187938685436698624",
   "text":"Very interesting for computing science students http:\/\/t.co\/xp4lq2Y1 #csstudents",
   "source":"web",
   "truncated":false,
   "in_reply_to_status_id":null,
   "in_reply_to_status_id_str":null,
   "in_reply_to_user_id":null,
   "in_reply_to_user_id_str":null,
   "in_reply_to_screen_name":null,
   "user":{  
      "id":132681798,
      "id_str":"132681798",
      "name":"Charilaos Eristikos",
      "screen_name":"EristikosTigka",
      "location":"Glasgow, Scotland",
      "url":null,
      "description":"Technology Lives Between Us.",
      "protected":false,
      "followers_count":52,
      "friends_count":133,
      "listed_count":2,
      "created_at":"Tue Apr 13 22:28:19 +0000 2010",
      "favourites_count":1,
      "utc_offset":0,
      "time_zone":"Edinburgh",
      "geo_enabled":false,
      "verified":false,
      "statuses_count":64,
      "lang":"en",
      "contributors_enabled":false,
      "is_translator":false,
      "profile_background_color":"FFFFFF",
      "profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/209319404\/8.jpg",
      "profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/209319404\/8.jpg",
      "profile_background_tile":true,
      "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1632508237\/380352_2437786137920_1049741902_2747933_696669280_n_normal.jpg",
      "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1632508237\/380352_2437786137920_1049741902_2747933_696669280_n_normal.jpg",
      "profile_link_color":"00B093",
      "profile_sidebar_border_color":"9E9E9E",
      "profile_sidebar_fill_color":"DDEEF6",
      "profile_text_color":"000000",
      "profile_use_background_image":true,
      "show_all_inline_media":false,
      "default_profile":false,
      "default_profile_image":false,
      "following":null,
      "follow_request_sent":null,
      "notifications":null
   },
   "geo":null,
   "coordinates":null,
   "place":null,
   "contributors":null,
   "retweet_count":0,
   "entities":{  
      "hashtags":[  
         {  
            "text":"csstudents",
            "indices":[  
               69,
               80
            ]
         }
      ],
      "urls":[  
         {  
            "url":"http:\/\/t.co\/xp4lq2Y1",
            "expanded_url":"http:\/\/googleblog.blogspot.co.uk\/2012\/03\/new-kind-of-summer-job-open-source.html",
            "display_url":"googleblog.blogspot.co.uk\/2012\/03\/new-ki\u2026",
            "indices":[  
               48,
               68
            ]
         }
      ],
      "user_mentions":[  

      ]
   },
   "favorited":false,
   "retweeted":false,
   "possibly_sensitive":false
}

它是Twitter的脚本

Its a script for Twitter

推荐答案

这不是合法的json,它包含对象的列表".您可以通过以下方式解决此问题:

This is not legal json, it contains a "list" of objects. You can fix the issue like this:

result = client.DownloadString(url);    // Putting the JSON content of the URL into a string
result = @"{ ""data"" : ["+result+"]}";

JObject obj = JObject.Parse(result);

这篇关于在C#中解析JSON查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆