我想在C#中开发一个网站来解析JSON查询中的数据,以便以后使用它们。到目前为止,我已经找到了所有要使用的库,但经过多次尝试解析不起作用,我希望你的帮助。我使用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;
}
}
}
}
}
这是输出中显示的错误:
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的脚本
答案 0 :(得分:3)
这不是合法的json,它包含对象的“列表”。你可以解决这个问题:
result = client.DownloadString(url); // Putting the JSON content of the URL into a string
result = @"{ ""data"" : ["+result+"]}";
JObject obj = JObject.Parse(result);
答案 1 :(得分:0)
你的JSON不好。
在一些在线JSON解析器上查看它,如http://json.parser.online.fr/
答案 2 :(得分:0)
第86行关闭对象文字。然后应该来EOF,但你有另一个对象字面。你的意思是创建一个数组吗?