0

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.

Here is the C# code.

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

Here is the JSON code:

    {  
   "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
}

Its a script for Twitter

6
  • Check out the inner exception. Commented Aug 26, 2012 at 1:20
  • No; just check the exception message. Commented Aug 26, 2012 at 1:23
  • Additional text encountered after finished reading JSON content: ,. Path '', line 1, position 2148. This is the message. I did not see anything different Commented Aug 26, 2012 at 1:40
  • Please show us the JSON. Commented Aug 26, 2012 at 1:54
  • I have updated the question with the JSON code included Commented Aug 26, 2012 at 2:01

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

7 Comments

I did it but there is a different problem now: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Thats better but still, it does not print anything
Are you kidding? You are talking about Console.Writeline line? Take some initiative dude and bother to think.
you can try this Console.WriteLine("name: " + obj["data"][0]["name"]); But remember I've no idea what a Jobject is, and that is where your problem is. Have some balls and look at it in the debugger because you have reached the "totally lazy bad SO user" stage.
Dude relax I am newbie in C# and that's why i am asking. If you think that i am the lazy one, why did you answer if you do not know what's a JObject?
|
0

Your JSON is bad.
Check it on some online JSON parser like http://json.parser.online.fr/

1 Comment

Actually I found this code online, I did not write it myself. Thank you very much for the online JSON parser. I was looking for something similar, but because i am new to JSON, i did not know how to search for it.
0

Line 86 closes the object literal. Then should come EOF, but you have another object literal. Did you mean to create an array?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.