Xamarin Json parsing - json

Ι am having a json like this:
{"userTweetsList":[{"tweetId":1,"tweetData":"testing uploading from uwp","createdTime":1510822592529,"commentCount":0,"likeCount":1,"flagCount":1,"mediaUrl":"suburl","tweetUser":"geo.thomas","userLiked":true,"userFlagged":true},{"tweetId":2,"tweetData":"Testing tweet","createdTime":1510821224655,"commentCount":0,"likeCount":0,"flagCount":1,"mediaUrl":"suburl","tweetUser":"sreejith.sree","userLiked":false,"userFlagged":true}],"tweetUsersMapList":[{"geo.thomas":{"username":"geo.thomas","name":"geo thomas ","profileImage":null}},{"sreejith.sree":{"username":"sreejith.sree","name":"sreejith sreenivasan","profileImage":null}}],"urlFileserverDynamic":"MyBaseUrl"}
My model class:
public class UserTweetResponse
{
public List<UserTweetsList> userTweetsList { get; set; }
}
public class UserTweetsList
{
public int tweetId { get; set; }
public string tweetData { get; set; }
public string createdTime { get; set; }
public int commentCount { get; set; }
public int likeCount { get; set; }
public int flagCount { get; set; }
public string mediaUrl { get; set; }
public string tweetUser { get; set; }
public bool userLiked { get; set; }
public bool userFlagged { get; set; }
public bool isMediaUrlNull { get { return string.IsNullOrEmpty(mediaUrl); } }
}
In my cs file, I link the listview with the userTweetsList, My listview name is ListView1.
ListView1.ItemsSource = userTweetResponse.userTweetsList;
I can access the all the data in the userTweetsList using {binding} property.
My problem is I need to access and show the name in ui based on the tweet user value. Suppose the "tweetUser":"geo.thomas" i need to access the
{"geo.thomas":{"username":"geo.thomas","name":"geo thomas ","profileImage":null}} inside the tweetUsersMapList, same way for "tweetUser":"sreejith.sree"
i need the data
{"sreejith.sree":{"username":"sreejith.sree","name":"sreejith sreenivasan","profileImage":null}}
For this I change the model like this:
public class UserTweetResponse
{
public List<UserTweetsList> userTweetsList { get; set; }
public List<TweetUsersMapList> tweetUsersMapList { get; set; }
}
public class UserTweetsList
{
public int tweetId { get; set; }
public string tweetData { get; set; }
public string createdTime { get; set; }
public int commentCount { get; set; }
public int likeCount { get; set; }
public int flagCount { get; set; }
public string mediaUrl { get; set; }
public string tweetUser { get; set; }
public bool userLiked { get; set; }
public bool userFlagged { get; set; }
public bool isMediaUrlNull { get { return string.IsNullOrEmpty(mediaUrl); } }
}
public class TweetUsersMapList
{
public string username { get; set; }
public string name { get; set; }
public string profileImage { get; set; }
}
But i don't know how to do the rest part. How can i link the listview with tweetUsersMapList? Based on the tweetUser name how can i pick the name and show that in the ui?
Is it possible to parse this type of complex json?
Anybody, please suggest a solution
Thanks in advance...

public class UserTweetsList
{
public int tweetId { get; set; }
public string tweetData { get; set; }
public object createdTime { get; set; }
public int commentCount { get; set; }
public int likeCount { get; set; }
public int flagCount { get; set; }
public string mediaUrl { get; set; }
public string tweetUser { get; set; }
public bool userLiked { get; set; }
public bool userFlagged { get; set; }
}
public class GeoThomas
{
public string username { get; set; }
public string name { get; set; }
public object profileImage { get; set; }
}
public class SreejithSree
{
public string username { get; set; }
public string name { get; set; }
public object profileImage { get; set; }
}
public class TweetUsersMapList
{
public GeoThomas __invalid_name__geo.thomas { get; set; }
public SreejithSree __invalid_name__sreejith.sree { get; set; }
}
public class RootObject
{
public List<UserTweetsList> userTweetsList { get; set; }
public List<TweetUsersMapList> tweetUsersMapList { get; set; }
public string urlFileserverDynamic { get; set; }
}
I have used Json2csharp to generate these classes...

Related

How to write the outputBuffer for the following class of data

JavaScriptSerializer js = new JavaScriptSerializer();
List<Root> Person = js.Deserialize<List<Root>>(jsonFileContent);
foreach (Root Person1 in Person)
{
Output0Buffer.AddRow();
Output0Buffer.id = Person1.id;
}
public class OfficeLocation
{
public string office { get; set; }
public string telephone { get; set; }
}
public class BiographyEducation
{
public string school { get; set; }
public List<object> description { get; set; }
}
public class Person
{
public int id { get; set; }
public string permalink { get; set; }
public string employeeID { get; set; }
public string firstName { get; set; }
public string middleName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public List<OfficeLocation> officeLocations { get; set; }
public string position { get; set; }
public List<string> capabilities { get; set; }
public string biographyNarrative { get; set; }
public List<BiographyEducation> biographyEducation { get; set; }
public List<string> biographyBarAdmissions { get; set; }
public string biographyMemberships { get; set; }
}
public class Root
{
internal string id;
public Person Person { get; set; }
}
}
I tried output0buffer using the root class but it will not work.

How to parse json with object and List for the same attribute? C#

On trying to parse JSON, I got an error about types. I have this code:
public class Event
{
[JsonProperty("eventKey")]
public int EventKey { get; set; }
[JsonProperty("displayOrder")]
public int DisplayOrder { get; set; }
[JsonProperty("subTypeKey")]
public int SubTypeKey { get; set; }
[JsonProperty("eventName")]
public string EventName { get; set; }
[JsonProperty("eventNameTranslated")]
public string EventNameTranslated { get; set; }
[JsonProperty("eventStatus")]
public string EventStatus { get; set; }
[JsonProperty("eventSort")]
public string EventSort { get; set; }
[JsonProperty("eventDateTime")]
public DateTime EventDateTime { get; set; }
[JsonProperty("classSortCode")]
public string ClassSortCode { get; set; }
[JsonProperty("typeFlagCode")]
public string TypeFlagCode { get; set; }
}
public class Events
{
[JsonProperty("num")]
public int Num { get; set; }
[JsonProperty("event")]
public IList<Event> Event { get; set; }
}
public class Subtype
{
[JsonProperty("subTypeKey")]
public int SubTypeKey { get; set; }
[JsonProperty("subTypeName")]
public string SubTypeName { get; set; }
[JsonProperty("displayOrder")]
public int DisplayOrder { get; set; }
[JsonProperty("events")]
public Events Events { get; set; }
}
public class Subtypes
{
[JsonProperty("num")]
public int Num { get; set; }
[JsonProperty("subtype")]
public Subtype Subtype { get; set; }
}
But I am getting error, because it can't parse it, cause sometimes it have type as List, sometimes as object. See screenshot (subtype and event).
How can I change my code to support different types?
Can you help me with this? Or what I need to read, to solve my problem?

I am a new bee with JSON and I don't know how to perform serialization and deserialization in windows phone

get links will be like this - http//192.156.120.192/Projects/cricket/index.php/api/api
public class Player
{
public int playerId { get; set; }
public string playerName { get; set; }
public string specialization { get; set; }
public string battingHand { get; set; }
public string bowlingHand { get; set; }
public string bowlingType { get; set; }
public object genericId { get; set; }
public int homeTeamId { get; set; }
public int eligibleTeams { get; set; }
public object imageUrl { get; set; }
}
public class RootObject
{
public string methodName { get; set; }
public int errorCode { get; set; }
public string errorMessage { get; set; }
public List<Player> players { get; set; }
}
I am using Json.NET and it is super easy
// create a RootObject object
//..
string serializedRoot = JsonConvert.SerializeObject(myRootObject);
RootObject deserializedRoot = JsonConvert.DeserializeObject<Player>(serializedRoot);

Getting EF 4.1 Code First project working

I am building a simple application with EF 4.1 Code First, but I am stuck. Here's an ERD of my application Domain Model:
In Visual Studio 2010, I have a solution with two projects in it. One project is to house the Domain Model, the other is an MVC3 Web Application to house the application logic.
In the Class Libraries (Project 1), I have the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace QuotesDomain
{
public class QuoteContext : DbContext
{
public DbSet<Quote> Quotes { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Language> Languages { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<QTBridge> QTBridge { get; set; }
}
public class Quote
{
public int Id { get; set; }
[Required, MaxLength(500)]
public string Body { get; set; }
public int Likes { get; set; }
[Required]
public bool isApproved { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
[Required]
public virtual Author Author { get; set; }
[Required]
public virtual Language Language { get; set; }
public virtual ICollection<QTBridge> TagsBridge { get; set; }
}
public class Author
{
public int Id { get; set; }
[Required, MaxLength(30), MinLength(2)]
public string FirstName { get; set; }
[Required, MaxLength(30), MinLength(2)]
public string LastName { get; set; }
[Required]
public DateTime DOB { get; set; }
public DateTime DOD { get; set; }
[Required, MaxLength(60), MinLength(2)]
public string Occupation { get; set; }
[Required, MaxLength(170), MinLength(5)]
public string WikiLink { get; set; }
public byte[] Image { get; set; }
[Required]
public bool isApproved { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public virtual ICollection<Quote> Quotes { get; set; }
}
public class Language
{
public int Id { get; set; }
[Required, MaxLength(20), MinLength(2)]
public string Name { get; set; }
public byte[] Image { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public virtual ICollection<Quote> Quotes { get; set; }
}
public class Tag
{
public int Id { get; set; }
[Required, MaxLength(40), MinLength(2)]
public string Name { get; set; }
public byte[] Image { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public virtual ICollection<QTBridge> QuotesBridge { get; set; }
}
public class QTBridge
{
public int Id { get; set; }
public int QuoteId { get; set; }
public int TagId { get; set; }
}
}
I've been watching some video tutorials, and the above looks good to me, but when I try and run the application, I get the following error:
Are my Code First classes correct based on the ERD Diagram? What do I need to do to solve the error?
Here's the full listing of code for Entities and configuration.
public class QuoteContext : DbContext
{
public DbSet<Quote> Quotes { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Language> Languages { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//This will create create a join table "QuoteTags"
modelBuilder.Entity<Quote>().HasMany(q => q.Tags)
.WithMany(t => t.Quotes);
}
}
public class Quote
{
public int Id { get; set; }
[Required, MaxLength(500)]
public string Body { get; set; }
public int Likes { get; set; }
[Required]
public bool IsApproved { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public int AuthorId { get; set; }
[ForeignKey("AuthorId")]
public virtual Author Author { get; set; }
public int LanguageId { get; set; }
[ForeignKey("LanguageId")]
public virtual Language Language { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Author
{
public int Id { get; set; }
[Required, MaxLength(30), MinLength(2)]
public string FirstName { get; set; }
[Required, MaxLength(30), MinLength(2)]
public string LastName { get; set; }
[Required]
public DateTime DOB { get; set; }
public DateTime DOD { get; set; }
[Required, MaxLength(60), MinLength(2)]
public string Occupation { get; set; }
[Required, MaxLength(170), MinLength(5)]
public string WikiLink { get; set; }
public byte[] Image { get; set; }
[Required]
public bool IsApproved { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public virtual ICollection<Quote> Quotes { get; set; }
}
public class Language
{
public int Id { get; set; }
[Required, MaxLength(20), MinLength(2)]
public string Name { get; set; }
public byte[] Image { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public virtual ICollection<Quote> Quotes { get; set; }
}
public class Tag
{
public int Id { get; set; }
[Required, MaxLength(40), MinLength(2)]
public string Name { get; set; }
public byte[] Image { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
public virtual ICollection<Quote> Quotes{ get; set; }
}

How can I parse the following JSON for WP7 and bind to a listbox?

I'm having some trouble binding this json to a ListBox...The problem seems to happen in the ActivityTrack class/object of the JSON...i'm not sure what is not meshing...
{"collection":[{"type":"track","created_at":"2011/09/18 14:04:00 +0000","origin":{"id":23606164,"created_at":"2011/09/18 14:03:59 +0000","user_id":222927,"duration":342465,"commentable":true,"state":"finished","sharing":"public","tag_list":"garage usgarage soulfulhouse house deephouse deep","permalink":"pablo-cortez-when-i-need-u","description":"","streamable":true,"downloadable":false,"genre":"UK Garage","release":"","purchase_url":null,"label_id":null,"label_name":"","isrc":"","video_url":null,"track_type":"demo","key_signature":"","bpm":null,"title":"Pablo Cortez - When I Need U (Back To 90s Mix)","release_year":null,"release_month":null,"release_day":null,"original_format":"mp3","license":"all-rights-reserved","uri":"https://api.soundcloud.com/tracks/23606164","permalink_url":"http://soundcloud.com/pablocortez/pablo-cortez-when-i-need-u","artwork_url":null,"waveform_url":"http://w1.sndcdn.com/wuiZilHRZhl6_m.png","user":{"id":222927,"permalink":"pablocortez","username":"Pablo Cortez","uri":"https://api.soundcloud.com/users/222927","permalink_url":"http://soundcloud.com/pablocortez","avatar_url":"http://i1.sndcdn.com/avatars-000000771958-y059w8-large.jpg?ca13f03"},"stream_url":"https://api.soundcloud.com/tracks/23606164/stream","user_playback_count":1,"user_favorite":false,"playback_count":28,"download_count":0,"favoritings_count":0,"comment_count":2,"attachments_uri":"https://api.soundcloud.com/tracks/23606164/attachments","sharing_note":{"text":"My new sounds","created_at":"2011/09/18 14:03:59 +0000"}},"tags":"affiliated"}],"next_href":"https://api.soundcloud.com/me/activities/track.json?cursor=86db5e5e-e1fe-11e0-9c69-0f0dad493cfc\\u0026limit=1","future_href":"https://api.soundcloud.com/me/activities/track?uuid%5Bto%5D=2d9c67ee-e22f-11e0-94fa-45aa16adeba3"}
Here is the class structure which i am trying to bind the JSON too
public class Activities
{
public ActivityTrack [] activities { get; set; }
public string next_href { get; set; }
public string future_href { get; set; }
}
public class ActivityTrack
{
public string type { get; set; }
public string created_at { get; set; }
public OriginActivityTrack origin { get; set; }
public string tags { get; set; }
}
public class OriginActivityTrack
{
public string id { get; set; }
public string created_at { get; set; }
public string user_id { get; set; }
public string duration { get; set; }
public string commentable { get; set; }
public string state { get; set; }
public string sharing { get; set; }
public string tag_list { get; set; }
public string permalink { get; set; }
public string description { get; set; }
public string streamable { get; set; }
public string downloadable { get; set; }
public string genre { get; set; }
public string release { get; set; }
public string purchase_url { get; set; }
public string label_id { get; set; }
public string label_name { get; set; }
public string isrc { get; set; }
public string video_url { get; set; }
public string track_type { get; set; }
public string key_signature { get; set; }
public string bpm { get; set; }
public string title { get; set; }
public string release_year { get; set; }
public string release_month { get; set; }
public string release_day { get; set; }
public string original_format { get; set; }
public string license { get; set; }
public string uri { get; set; }
public string permalink_url { get; set; }
public string artwork_url { get; set; }
public string waveform_url { get; set; }
public SmallUser user { get; set; }
public string stream_url { get; set; }
public string user_playback_count { get; set; }
public string user_favorite { get; set; }
public string playback_count { get; set; }
public string download_count { get; set; }
public string favoritings_count { get; set; }
public string comment_count { get; set; }
public string attachments_uri { get; set; }
public SharingNote sharing_note;
}
public class SharingNote
{
public string text { get; set; }
public string created_at { get; set; }
}
public class SmallUser
{
public string id { get; set; }
public string permalink { get; set; }
public string username { get; set; }
public string uri { get; set; }
public string permalink_url { get; set; }
public string avatar_url { get; set; }
}
This is the current code for binding to listbox:
private void ReadCallbackDashboard(IAsyncResult asynchronousResult)
{
lock (locker)
{
try
{
HttpWebRequest request =
(HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response =
(HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader streamReader1 =
new StreamReader(response.GetResponseStream()))
{
string resultString = streamReader1.ReadToEnd();
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(resultString)))
{
if (ms != null)
{
var ser = new DataContractJsonSerializer(typeof(Activities));
Activities obj = (Activities)ser.ReadObject(ms);
userDashboardActivities = null;
userDashboardActivities = new ObservableCollection<ActivityTrack>();
for (int i = 0; i < obj.activities.Length; ++i)
{
userDashboardActivities.Add(obj.activities[i]);
}
if (userDashboardActivities.Count() == 0)
{
messageDashboard = "No Tracks Found";
UIThread.Invoke(() => mainMessage.Text = messageDashboard);
}
else
{
messageDashboard = "";
UIThread.Invoke(() => mainMessage.Text = messageDashboard);
UIThread.Invoke(() => dashboardBox.ItemsSource = userDashboardActivities);
}
}
}
}
}
catch (WebException we)
{
UIThread.Invoke(() => MessageBox.Show("Could not retrieve the latest. Internet down? Try a refresh."));
}
}
}
please check it out! Thanks in advance!
Answer as discovered in the comments.
You have called your property activities but the json calls it collection.