【问题标题】:Parse JSON query in C#在 C# 中解析 JSON 查询
【发布时间】:2012-08-26 01:18:12
【问题描述】:

我想用 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 的脚本

【问题讨论】:

  • 检查内部异常。
  • 否;只需检查异常消息。
  • 阅读完 JSON 内容后遇到的附加文本:,.路径 '',第 1 行,位置 2148。这是消息。我没有看到任何不同
  • 请向我们展示 JSON。
  • 我已经更新了包含 JSON 代码的问题

标签: c# .net json parsing json.net


【解决方案1】:

这不是合法的 json,它包含对象的“列表”。您可以这样解决问题:

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

JObject obj = JObject.Parse(result);

【讨论】:

  • 我做到了,但现在有一个不同的问题:Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
  • 这样更好,但仍然不打印任何内容
  • 你在开玩笑吗?你说的是 Console.Writeline 行吗?采取一些主动的家伙,不厌其烦地思考。
  • 你可以试试这个Console.WriteLine("name: " + obj["data"][0]["name"]); 但请记住,我不知道 Jobject 是什么,这就是你的问题所在。试一试,在调试器中查看它,因为您已达到“完全懒惰的不良 SO 用户”阶段。
  • 伙计放松我是 C# 的新手,这就是我问的原因。如果你认为我是一个懒惰的人,你为什么不知道什么是 JObject 来回答?
【解决方案2】:

你的 JSON 很糟糕。
在一些在线 JSON 解析器上检查它,例如 http://json.parser.online.fr/

【讨论】:

  • 其实我是在网上找到这段代码的,不是我自己写的。非常感谢在线 JSON 解析器。我一直在寻找类似的东西,但因为我是 JSON 新手,所以我不知道如何搜索它。
【解决方案3】:

第 86 行关闭了对象字面量。然后应该是 EOF,但是你有另一个对象文字。你的意思是创建一个数组吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    相关资源
    最近更新 更多