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

dey.shin
  • 990
  • 10
  • 21
Eristikos
  • 485
  • 4
  • 9
  • 24

3 Answers3

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);
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • 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.` – Eristikos Aug 26 '12 at 02:20
  • Thats better but still, it does not print anything – Eristikos Aug 26 '12 at 12:45
  • Are you kidding? You are talking about Console.Writeline line? Take some initiative dude and bother to think. – Hogan Aug 26 '12 at 16:49
  • 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. – Hogan Aug 26 '12 at 16:52
  • 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? – Eristikos Aug 27 '12 at 00:49
  • I answered because I want to help. Most people won't know what it is since it is a random library not a standard C# one. `Newtonsoft.Json.Linq` is not a standard C# library. – Hogan Aug 27 '12 at 18:07
  • Yes i know that but most of the question i have seen here in stackoverflow.com are about this library and this is the reason why i tried it. Anyway thanks for your help – Eristikos Aug 28 '12 at 00:10
0

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

zendar
  • 13,384
  • 14
  • 59
  • 75
  • 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. – Eristikos Aug 26 '12 at 02:14
0

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

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141