JSON парсер найденых твитов из твитера - C#
Формулировка задачи:
Всем привет, с помощью сервиса твитера получаю информацию в JSON по запросу. Но что-то не могу получить уровень
geo
(возвращается null) в этом JSON'e и подразумеваю что не правильно описал уровень coordinates, подскажите где я мог ошибиться, ниже приведу описанную мной структуру JSON файла. Файл прилагаю в формате txt /*Описание структуры Json*/
[JsonObject]
public struct JsonUserObejct
{
[JsonProperty]
public Statuses[] statuses { get; set; }
public Search_metadata search_metadata { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public struct Statuses
{
[JsonProperty]
public Metadata metadata { get; set; }
[JsonProperty]
//[JsonConverter(typeof(IsoDateTimeConverter))]
public string created_at { get; set; }
[JsonProperty]
public decimal id { get; set; }
[JsonProperty]
public string id_str { get; set; }
[JsonProperty]
public string text { get; set; }
[JsonProperty]
public string source { get; set; }
[JsonProperty]
public bool truncated { get; set; }
[JsonProperty]
public decimal? in_reply_to_status_id { get; set; }
[JsonProperty]
public string in_reply_to_status_id_str { get; set; }
//[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
[JsonProperty]
public decimal? in_reply_to_user_id { get; set; }
[JsonProperty]
public string in_reply_to_user_id_str { get; set; }
[JsonProperty]
public string in_reply_to_screen_name { get; set; }
[JsonProperty]
public User user { get; set; }
[JsonProperty]
public Geo? geo { get; set; }
[JsonProperty]
public Coordinates? coordinates { get; set; }
[JsonProperty]
public Place? place { get; set; }
[JsonProperty]
public string contributors { get; set; }
[JsonProperty]
public int retweet_count { get; set; }
[JsonProperty]
public int favorite_count { get; set; }
[JsonProperty]
public Entities entities { get; set; }
[JsonProperty]
public bool favorited { get; set; }
[JsonProperty]
public bool retweeted { get; set; }
[JsonProperty]
public string lang { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public struct Search_metadata
{
public double completed_in { get; set; }
public decimal max_id { get; set; }
public string max_id_str { get; set; }
public string query { get; set; }
public string refresh_url { get; set; }
public int count { get; set; }
public int since_id { get; set; }
public string since_id_str { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public struct Metadata
{
[JsonProperty]
public string iso_language_code {get; set;}
[JsonProperty]
public string result_type {get; set;}
}
[JsonObject(MemberSerialization.OptIn)]
public struct User
{
[JsonProperty]
public decimal id { get; set; }
[JsonProperty]
public string id_str { get; set; }
[JsonProperty]
public string name { get; set; }
[JsonProperty]
public string screen_name { get; set; }
[JsonProperty]
public string location { get; set; }
[JsonProperty]
public string profile_location { get; set; }
[JsonProperty]
public string description { get; set; }
[JsonProperty]
public string url { get; set; }
[JsonProperty]
public Entities entities { get; set; }
[JsonProperty]
public bool protect { get; set; }
[JsonProperty]
public int followers_count { get; set; }
[JsonProperty]
public int friends_count { get; set; }
[JsonProperty]
public int listed_count { get; set; }
[JsonProperty]
//[JsonConverter(typeof(IsoDateTimeConverter))]
public string created_at { get; set; }
[JsonProperty]
public int favourites_count { get; set; }
[JsonProperty]
public int? utc_offset { get; set; }
[JsonProperty]
public string time_zone { get; set; }
[JsonProperty]
public bool geo_enabled { get; set; }
[JsonProperty]
public bool verified { get; set; }
[JsonProperty]
public string statuses_count { get; set; }
[JsonProperty]
public string lang { get; set; }
[JsonProperty]
public bool contributors_enabled { get; set; }
[JsonProperty]
public bool is_translator { get; set; }
[JsonProperty]
public bool is_translation_enabled { get; set; }
[JsonProperty]
public string profile_background_color { get; set; }
[JsonProperty]
public string profile_background_image_url { get; set; }
[JsonProperty]
public string profile_background_image_url_https { get; set; }
[JsonProperty]
public bool profile_background_tile { get; set; }
[JsonProperty]
public string profile_image_url { get; set; }
[JsonProperty]
public string profile_image_url_https { get; set; }
[JsonProperty]
public string profile_link_color { get; set; }
[JsonProperty]
public string profile_sidebar_border_color { get; set; }
[JsonProperty]
public string profile_sidebar_fill_color { get; set; }
[JsonProperty]
public string profile_text_color { get; set; }
[JsonProperty]
public bool profile_use_background_image { get; set; }
[JsonProperty]
public bool default_profile { get; set; }
[JsonProperty]
public bool default_profile_image { get; set; }
[JsonProperty]
public bool? following { get; set; }
[JsonProperty]
public bool? follow_request_sent { get; set; }
[JsonProperty]
public bool? notifications { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public struct Entities
{
public Description description { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public struct Description
{
public string[] urls { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public struct Geo
{
public string type { get; set; }
public Coord[] coordinates { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public struct Coord
{
public double[] coordinates;
}
[JsonObject(MemberSerialization.OptIn)]
public struct Coordinates
{
public string type { get; set; }
public Coord coordinates;
}
[JsonObject(MemberSerialization.OptIn)]
public struct Place
{
public string id { get; set; }
public string url { get; set; }
public string place_type { get; set; }
public string name { get; set; }
public string full_name { get; set; }
public string country_code { get; set; }
public string country { get; set; }
public string[] contained_within { get; set; }
public Bounding_Box bounding_box { get; set; }
//attributes
}
[JsonObject(MemberSerialization.OptIn)]
public struct Bounding_Box
{
public string type { get; set; }
public Coordinates[] coordinates { get; set; }
}
//[JsonObject(MemberSerialization.OptIn)]
//public struct Entities
//{
// public string[] hashtags { get; set; }
// public string[] symbols { get; set; }
// public string[] user_mentions { get; set; }
// public string[] urls { get; set; }
//}
class Program
{
static void Main(string[] args)
{
string jsonFileName = @"C:\tweet.json";
string jsonObj = File.ReadAllText(jsonFileName, Encoding.UTF8);
JsonUserObejct jUserObject = JsonConvert.DeserializeObject<JsonUserObejct>(jsonObj);
//Console.WriteLine(jUserObject.statuses[0].coordinates.coordinates[0].ToString());
Console.WriteLine("-=:END:=-");
Console.ReadKey(true);
}
}Решение задачи: «JSON парсер найденых твитов из твитера»
textual
Листинг программы
string jsonFileName = @"C:\tweet.json";
string jsonObj = File.ReadAllText(jsonFileName, Encoding.UTF8);
var obj = JObject.Parse(jsonObj);
foreach (var tweet in obj["statuses"])
{
Console.WriteLine("Text: " + tweet["text"]);
Console.WriteLine("User ID: " + tweet["user"]["id"]);
Console.WriteLine("User: " + tweet["user"]["name"]);
Console.WriteLine("Location: " + tweet["user"]["location"]);
Console.WriteLine("Geo type: " + tweet["geo"]["type"]);
Console.WriteLine("Geo coordinates: " + " Lat=" + tweet["geo"]["coordinates"][0] + " Lon=" + tweet["geo"]["coordinates"][1]);
Console.WriteLine("Coordinates Type: " + tweet["coordinates"]["type"]);
Console.WriteLine("Coordinates LatLon: " + " Lat=" + tweet["coordinates"]["coordinates"][0] + " Lon=" + tweet["coordinates"]["coordinates"][1]);
Console.WriteLine("Place type: " + tweet["place"]["place_type"]);
Console.WriteLine("Place Name: " + tweet["place"]["name"]);
Console.WriteLine("Place FullName: " + tweet["place"]["full_name"]);
Console.WriteLine("Bounding Box type: " + tweet["place"]["bounding_box"]["type"]);
foreach (var coord in tweet["place"]["bounding_box"]["coordinates"])
{
Console.WriteLine("coord: " + coord);
}
}