I will appologise in advance if I am being really stupid here, but I cant find the right syntax to extract some data from a JSON return. The following is the returned JSON data:
{
"version":"1.0",
"encoding":"UTF-8",
"feed":{
"xmlns":"http://www.w3.org/2005/Atom",
"xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/",
"xmlns$gsx":"http://schemas.google.com/spreadsheets/2006/extended",
"id":{
"$t":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic"
},
"updated":{
"$t":"2012-12-03T10:33:13.778Z"
},
"category":[
{
"scheme":"http://schemas.google.com/spreadsheets/2006",
"term":"http://schemas.google.com/spreadsheets/2006#list"
}
],
"title":{
"type":"text",
"$t":"Sheet1"
},
"link":[
{
"rel":"alternate",
"type":"text/html",
"href":"https://spreadsheets.google.com/pub?key\u003d0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE"
},
{
"rel":"http://schemas.google.com/g/2005#feed",
"type":"application/atom+xml",
"href":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic"
},
{
"rel":"self",
"type":"application/atom+xml",
"href":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic?alt\u003djson"
}
],
"author":[
{
"name":{
"$t":"rourkie"
},
"email":{
"$t":"rourkie#gmail.com"
}
}
],
"openSearch$totalResults":{
"$t":"1"
},
"openSearch$startIndex":{
"$t":"1"
},
"entry":[
{
"id":{
"$t":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic/cn6ca"
},
"updated":{
"$t":"2012-12-03T10:33:13.778Z"
},
"category":[
{
"scheme":"http://schemas.google.com/spreadsheets/2006",
"term":"http://schemas.google.com/spreadsheets/2006#list"
}
],
"title":{
"type":"text",
"$t":"5872.64"
},
"content":{
"type":"text",
"$t":"change: 3.6"
},
"link":[
{
"rel":"self",
"type":"application/atom+xml",
"href":"https://spreadsheets.google.com/feeds/list/0AhySzEddwIC1dEN6bnNQYkRlVE50RlBRLUQ5YlZhNUE/1/public/basic/cn6ca"
}
]
}
]
}
}
I am trying to extract the "change" figure, with the following:
feed.entry[4].content.$t
but it just keeps returning an error.
Can anyone shed some light on what I am doing wrong??
Thanks
JSONLint - http://jsonlint.com/ - is pretty handy for this.
The JSON you posted only has one object in the entry array (unless you just posted it as an example)...so it would be:
feed.entry[0].content.$t
Using json2csharp.com (http://json2csharp.com/) You can paste in json and it will give you a class matching the json allowing you to easily parse it. Check How can I parse JSON with C#? on how to use JsonConvert. I believe you can find it in nuget packages. Keep in mind the class I have pasted below will not work directly, because of the naming of some of the fields in the json. You may have to rename them manually and map them.
public class Id
{
public string __invalid_name__$t { get; set; }
}
public class Updated
{
public string __invalid_name__$t { get; set; }
}
public class Category
{
public string scheme { get; set; }
public string term { get; set; }
}
public class Title
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Link
{
public string rel { get; set; }
public string type { get; set; }
public string href { get; set; }
}
public class Name
{
public string __invalid_name__$t { get; set; }
}
public class Email
{
public string __invalid_name__$t { get; set; }
}
public class Author
{
public Name name { get; set; }
public Email email { get; set; }
}
public class OpenSearchTotalResults
{
public string __invalid_name__$t { get; set; }
}
public class OpenSearchStartIndex
{
public string __invalid_name__$t { get; set; }
}
public class Id2
{
public string __invalid_name__$t { get; set; }
}
public class Updated2
{
public string __invalid_name__$t { get; set; }
}
public class Category2
{
public string scheme { get; set; }
public string term { get; set; }
}
public class Title2
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Content
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Link2
{
public string rel { get; set; }
public string type { get; set; }
public string href { get; set; }
}
public class Entry
{
public Id2 id { get; set; }
public Updated2 updated { get; set; }
public List<Category2> category { get; set; }
public Title2 title { get; set; }
public Content content { get; set; }
public List<Link2> link { get; set; }
}
public class Feed
{
public string xmlns { get; set; }
public string __invalid_name__xmlns$openSearch { get; set; }
public string __invalid_name__xmlns$gsx { get; set; }
public Id id { get; set; }
public Updated updated { get; set; }
public List<Category> category { get; set; }
public Title title { get; set; }
public List<Link> link { get; set; }
public List<Author> author { get; set; }
public OpenSearchTotalResults __invalid_name__openSearch$totalResults { get; set; }
public OpenSearchStartIndex __invalid_name__openSearch$startIndex { get; set; }
public List<Entry> entry { get; set; }
}
public class RootObject
{
public string version { get; set; }
public string encoding { get; set; }
public Feed feed { get; set; }
}
Related
This question already has answers here:
How to Deserialize JSON data? C#
(5 answers)
Closed 3 years ago.
I have two different class using that I am sending json response through API to another application, in short I am getting following json response :
{
"lmob_Forms":[
{
"Fields":"Certificate Name",
"Validation":"R",
"TabIndex":"1",
"FieldType":"DropdownList",
"DateFormate":"",
"Details":null
},
],
"drop_Salutation":[
{
"id":1,
"idvalue":"Kumari"
},
{
"id":2,
"idvalue":"Mrs"
},
{
"id":3,
"idvalue":"Ms"
}
]
}
I have searched regarding the same but not able to solve this problem, Need help, Thanks In Advance :)
First of all create a model of your json object.
public class LmobForm
{
public string Fields { get; set; }
public string Validation { get; set; }
public string TabIndex { get; set; }
public string FieldType { get; set; }
public string DateFormate { get; set; }
public object Details { get; set; }
}
public class DropSalutation
{
public int id { get; set; }
public string idvalue { get; set; }
}
public class RootObject
{
public List<LmobForm> lmob_Forms { get; set; }
public List<DropSalutation> drop_Salutation { get; set; }
}
Now, simply use Newtonsoft.Json to deserialize your JSON object as:
var myObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
using json2csharp
public class LmobForm
{
public string Fields { get; set; }
public string Validation { get; set; }
public string TabIndex { get; set; }
public string FieldType { get; set; }
public string DateFormate { get; set; }
public object Details { get; set; }
}
public class DropSalutation
{
public int id { get; set; }
public string idvalue { get; set; }
}
public class RootObject
{
public List<LmobForm> lmob_Forms { get; set; }
public List<DropSalutation> drop_Salutation { get; set; }
}
I have been having a hard time trying to figure this out that I've just about torn out all my hair now.
Using this section of code (Added a var result that I looking at with a stoppoint):
public async Task<string> GetHttpSPContentWithToken(string url, string token)
{
var httpClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response;
try
{
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
response = await httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<SharePointListItems.Fields>(content);
return content;
}
catch (Exception ex)
{
return ex.ToString();
}
}
The content that I receive is this (updated getting rid of extra information):
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('root')/lists('FBA0AB63-8453-4BB9-AA17-142A5D72A50D')/items/$entity",
"#odata.etag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"createdDateTime": "2018-08-07T14:28:47Z",
"eTag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"id": "1",
"lastModifiedDateTime": "2018-08-07T14:28:47Z",
"webUrl": "https://XXXX.sharepoint.com/Lists/TestList/1_.000",
"createdBy": {
"user": {
"email": "XXXX#XXXX.onmicrosoft.com",
"id": "b5f81cc6-f8b7-46b7-8e10-6ce1b9689c23",
"displayName": "TK"
}
},
"lastModifiedBy": {
"user": {
"email": "XXXX#XXXX.onmicrosoft.com",
"id": "b5f81cc6-f8b7-46b7-8e10-6ce1b9689c23",
"displayName": "TK"
}
},
"parentReference": {},
"contentType": {
"id": "0x010001403BD420356E4ABE3B63E5AEC0713D"
},
"fields#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('root')/lists('FBA0AB63-8453-4BB9-AA17-142A5D72A50D')/items('1')/fields/$entity",
"fields": {
"#odata.etag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"Title": "1",
"UserName": "TK",
"UserAge": "47",
"UserTitle": "Developer"
}
}
I just want the values forUserAge, UserName, and UserTitle to put each into a textbox, but not sure how to pull them out.
I am pretty sure that I need to set up a class of some sort, but it is the #odata parts that are breaking my back.
Everything that I have tried just gives me back a null value. I see the value there, just not sure how to parse/pull it out.
I have looked at this (updated):
using Newtonsoft.Json;
using System;
public class SharePointListItems
{
public class UserCreated
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class CreatedBy
{
public UserCreated user { get; set; }
}
public class UserModified
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class LastModifiedBy
{
public UserModified user { get; set; }
}
public class ParentReference
{
}
public class ContentType
{
public string id { get; set; }
}
public class Fields
{
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
public string Title { get; set; }
public string UserName { get; set; }
public string UserAge { get; set; }
public string UserTitle { get; set; }
}
public class RootObject
{
[JsonProperty("#odata.context")]
public string ODataContext { get; set; }
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
public DateTime createdDateTime { get; set; }
public string eTag { get; set; }
public string id { get; set; }
public DateTime lastModifiedDateTime { get; set; }
public string webUrl { get; set; }
public CreatedBy createdBy { get; set; }
public LastModifiedBy lastModifiedBy { get; set; }
public ParentReference parentReference { get; set; }
public ContentType contentType { get; set; }
[JsonProperty("fields#odata.context")]
public string FieldsODataContext { get; set; }
public Fields fields { get; set; }
}
}
But then I run into the issue that there is two [JsonProperty("#odata.etag")].
The [JsonProperty] custom attribute is added to the C# property that actually holds that value. Instead of putting the attribute on the Title or createDateTime property, you need to put them on their own properties:
public class RootObject
{
[JsonProperty("#odata.context")]
public string ODataContext { get; set; }
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
// No attribute needed here
public DateTime createdDateTime { get; set; }
// etc...
Also, you are trying to parse the content as a Fields class, but it is a RootObject; you need to use
JsonConvert.DeserializeObject<SharePointListItems.RootObject>(content)
To get the object.
Ι 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...
I have been looking for ages and found some similar questions but no answers which fixed my issue.
I have recently developed a Web API project with seperate server side and client side code. I am wanting to do some testing through apiary, mainly for posting data. This requires me to have a "sample" of the posted JSON. However i cannot seem to get a working sample (I am using Postman to test the JSON).
I am sending a ViewModel:
public class ExampleViewModel
{
public ExampleModel exampleModel { get; set; }
public String[] strings { get; set; }
}
With the model:
public class ExampleModel
{
public long ID { get; set; }
public string User { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public string Description { get; set; }
public bool Recieved { get; set; }
public bool CompleteRemotly { get; set; }
public bool CompleteInField { get; set; }
public string Email { get; set; }
public Nullable<DateTime> RecievedDate { get; set; }
public Nullable<DateTime> ScheduledFor { get; set; }
public Nullable<DateTime> CompleteOn { get; set; }
public string Priority { get; set; }
public string DiagnosticReports { get; set; }
public string CReference { get; set; }
public string JReference { get; set; }
}
The format the POST is expecting is:
Key: "evm",
Value: {APIProject.ViewModels.ExampleViewModel}
And:
APIProject.ViewModels.ExampleViewModel:
{
strings = [""],
exampleModel = {ExampleModel}
}
My latest attempt is:
{
"evm":{
"ExampleViewModel":{
"exampleModel":{
"ID":0,
"User":"Harrison",
"Title":"Other",
"Type":"Other",
"Description":"ygfvgufuf",
"Recieved":true,
"CompleteRemotly":false,
"CompleteInField":false,
"Email":null,
"RecievedDate":"\/Date(1458133155546)\/",
"ScheduledFor":null,
"CompleteOn":null,
"Priority":"Low",
"DiagnosticReports":null,
"CPA_Reference":null,
"Jira_Reference":null,
"strings":[]
},
"strings":[""]
}
}
}
Which returns:
Key: "evm",
Value: {APIProject.ViewModels.ExampleViewModel}
And:
APIProject.ViewModels.ExampleViewModel:
{
strings = null,
exampleModel = null
}
So i got this to work by sending the following JSON, I guess i was over complicating the situation.
{
"requestManagements":{
"RequestID":0,
"User":"Harrison",
"RequestTitle":"Other",
"RequestType":"Other",
"RequestDescription":"ygfvgufuf",
"RequestRecieved":true,
"RequestCompleteRemotly":false,
"RequestCompleteInField":false,
"Email":null,
"RequestRecievedDate":"\/Date(1458133155546)\/",
"ScheduledFor":null,
"CompleteOn":null,
"Priority":"Low",
"DiagnosticReports":null,
"CPA_Reference":null,
"Jira_Reference":null,
"RequestManagerImages":[]
},
"images":[""]
}
I am writing an mVC4 application and i would like to create a model to generate the following JSON file:
{
"area": {
"areaid": "1",
"venueid": "41",
"fnames": "12",
"s": [{"Id":1,"V":"0,1,2,2,1,1,1,0B-001,0,0,0,"},
{"Id":2,"V":"2,1,2,2,1,1,1,0B-001,0,0,0,"},
{"Id":3,"V":"3,1,2,2,1,1,1,0B-001,0,0,0,"}]
}
}
Something like that?
public class Area
{
[Key]
public int areaid { get; set; }
public int venueid { get; set; }
public int fnames { get; set; }
[ForeignKey("Id")]
public List<Book> s { get; set; }
}
public class Book
{
[Key]
public int Id { get; set; }
public string V { get; set; }
}
I think that should work. Here's the result from using Visual Studio 2013's Paste JSON as Classes feature...
public class Rootobject
{
public Area area { get; set; }
}
public class Area
{
public string areaid { get; set; }
public string venueid { get; set; }
public string fnames { get; set; }
public List<Book> s { get; set; }
}
public class Book
{
public int Id { get; set; }
public string V { get; set; }
}