I have a WebApi returning the following JSON which I am trying to deserialize to the object below
JSON OBJECT
{
"results": [{
"id": 123456,
"fullName": "Foo Bar",
"localName": null,
"jobPosition": "ME",
"jobCompanyName": "EXTRA",
"jobLocationCountry": "United States of America",
"jobLocationCity": "San Francisco",
"jobCountrySubdivision": "California",
"boards": [],
"restrictionsIndicator": false,
"personRestriction": null,
"jobRestriction": null
}, {
"id": 789101,
"fullName": "Foo Bar",
"localName": null,
"jobPosition": null,
"jobCompanyName": "Unknown",
"jobLocationCountry": "Unknown",
"jobLocationCity": "Unknown",
"jobCountrySubdivision": "Unknown",
"boards": [{
"companyId": 667525,
"companyName": "FOO BAR COMPANY",
"companyOffLimits": null,
"restrictionCategoryId": null
}
],
"restrictionsIndicator": false,
"personRestriction": null,
"jobRestriction": null
}
],
"totalCount": 2,
"pageNumber": 1,
"resultsPerPage": 100
}
C# Classes
public class Rootobject
{
public Result[] results { get; set; }
public int totalCount { get; set; }
public int pageNumber { get; set; }
public int resultsPerPage { get; set; }
}
public class Result
{
public int id { get; set; }
public string fullName { get; set; }
public object localName { get; set; }
public string jobPosition { get; set; }
public string jobCompanyName { get; set; }
public string jobLocationCountry { get; set; }
public string jobLocationCity { get; set; }
public string jobCountrySubdivision { get; set; }
public Board[] boards { get; set; }
public bool restrictionsIndicator { get; set; }
public int? personRestriction { get; set; }
public int? jobRestriction { get; set; }
}
public class Board
{
public int companyId { get; set; }
public string companyName { get; set; }
public int? companyOffLimits { get; set; }
public object restrictionCategoryId { get; set; }
}
The DLL is a Portable Class Library which is .NET 4.5 and i have JSON.net(10.0.1) installed via nuget, but the portable library is connected to a xamarin IOS Project on a mac.
If the JSON being deserialized has no Boards it works out fine but if there is a Board then I receive the following message.
Unable to find a constructor to use for type Board. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'results[1].boards[0].companyId'
The Settings I am using are:
var settings = new Newtonsoft.Json.JsonSerializerSettings
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
};
I have tried the following ways to get it to serialize:
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(_jsonString, settings);
var jobject = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(_jsonString, new Rootobject());
i have tried the following
Put in a default constructor
Naming all the parameters for the class in a constructor
Adding the Attribute to the constructor
Changing the Boards to a List
Taking out the Boards Property
but there is still no joy. It will not deserialize for me.
I think you have to modify this
public class Board
{
public int companyId { get; set; }
public string companyName { get; set; }
public int? companyOffLimits { get; set; }
public object restrictionCategoryId { get; set; }
**public Board(){}**
}
also in other classes
or also change
public Board[] boards { get; set; }
to
public List<Board> boards { get; set; }
Try....
Related
I dont have any problems but i don't know how API call data Nested Values
My Web API have Controller, Models and DataAccess that i call by use sql to call data
I want API call JSON Data format similar as below :
[
{
"Agent_Code": "123456",
"Name": "Miss Sara Manger",
"NickName": "Sara",
"BirthDay": "19690825",
"CardID": "9999999999",
"Address": "870 Goldleaf Lane Lyndhurst NJ New Jersey 07071",
"Mobile": "000000000",
"Email": "utv9hgn3h0k#classesmail.com",
"Bank": [
{
"AcctName": "Miss Sara Manger",
"Acctno": "9999999999",
"Bank": "KBANK"
}
]
}
]
Controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Testdata.APIs
{
public class DashController : BaseApiController
{
[HttpPost]
public List<Models.AgentDto> TestJSON(Models.CountDashReq model)
{
DataAccess.DashDAL dal = new DataAccess.DashDAL();
List<Models.AgentDto> models = dal.TestJSON(model);
return models;
}
}
Model AgentDto And BankDto:
public class AgentDto
{
public string Agent_Code { get; set; }
public string Name { get; set; }
public string NickName { get; set; }
public string BirthDay { get; set; }
public string CardID { get; set; }
public string Address { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Bank { get; set; }
}
public class BankDto
{
public string AcctName { get; set; }
public string Acctno { get; set; }
public string Bank { get; set; }
}
DataAccess:
public List<Models.AgentDto> TestJSON(Models.CountDashReq model)
{
string sql = "[dbo].[tb_Json_Get]";
List<Models.AgentDto> result = new List<Models.AgentDto>();
if (model != null)
{
List<SqlParameter> reqParam = new List<SqlParameter>();
//reqParam.Add(new SqlParameter("#usrLogin", model.usrLogin));
DataSet ds = this.Execute(sql, CommandType.StoredProcedure, reqParam.ToArray(), false);
if (ds.Tables.Count > 0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
Models.AgentDto paramslist = DataAccessUtility.DataTableExtensions.AsEnumerable<Models.AgentDto>(row);
result.Add(paramslist);
}
}
}
return result;
}
Please help me to use Case-when Thank you very much
If you getting request JSON as:
[
{
"Agent_Code": "123456",
"Name": "Miss Sara Manger",
"NickName": "Sara",
"BirthDay": "19690825",
"CardID": "9999999999",
"Address": "870 Goldleaf Lane Lyndhurst NJ New Jersey 07071",
"Mobile": "000000000",
"Email": "utv9hgn3h0k#classesmail.com",
"Bank": [
{
"AcctName": "Miss Sara Manger",
"Acctno": "9999999999",
"Bank": "KBANK"
}
]
}
]
Then the CountDashReq class should be:
public class Bank
{
public string AcctName { get; set; }
public string Acctno { get; set; }
public string Bank { get; set; }
}
public class CountDashReq
{
public string Agent_Code { get; set; }
public string Name { get; set; }
public string NickName { get; set; }
public string BirthDay { get; set; }
public string CardID { get; set; }
public string Address { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public List<Bank> Bank { get; set; }
}
In controller method:
[HttpPost]
public List<Models.AgentDto> TestJSON(IList<Models.CountDashReq> model)
{
// Your code
}
I have a problem that an index I've made when queried is throwing an exception :
Could not read value for property: Members
Here is the index :
public class GroupsNameIdIndex : AbstractIndexCreationTask<CommunityDocument, GroupsNameIdIndex.Result>
{
public class Result
{
public string CommunityId { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public IList<string> Members { get; set; }
}
public GroupsNameIdIndex()
{
Map = communities => from community in communities
from newGroup in community.Groups
select new
{
CommunityId = community.Id.Replace("CommunityDocuments/", string.Empty),
newGroup.Id,
newGroup.Name,
newGroup.Members
};
StoreAllFields(FieldStorage.Yes);
}
}
This is the query :
var groupResult = session
.Query<GroupsNameIdIndex.Result, GroupsNameIdIndex>()
.Where(x => x.CommunityId == info.Id)
.AsProjection<GroupsNameIdIndex.Result>()
.ToList();
I only have 1 group in the document with relevant Id and the Members node is an empty list, not null. When I manually populate it with a single string, then the query runs fine. Why are empty lists not allowed? If this is a restriction, it makes indexing pointless for me, because I will have a lot of empty lists that are just going to make the application fail everywhere.
edit: Adding the class in case that has something to do with it:
public class CommunityGroup
{
public CommunityGroup()
{
Members = new List<string>();
MemberDetails = new List<MemberView>();
MemberNames = new List<string>();
}
public string Id { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public string Description { get; set; }
public bool AdminOnly { get; set; }
public bool NewsContribute { get; set; }
public bool AutoGroup { get; set; }
public int LowerAgeLimit { get; set; }
public int UpperAgeLimit { get; set; }
public string Gender { get; set; }
public IList<string> Members { get; set; }
public IList<string> MemberNames { get; set; }
public IList<MemberView> MemberDetails { get; set; }
public string RelatedCommunityId { get; set; }
public string CreatedBy { get; set; }
public bool SmartGroup { get; set; }
public IList<SmartGroupRules> AndRules { get; set; }
public IList<SmartGroupRules> OrRules { get; set; }
public string ParentGroup { get; set; }
public List<string> EntryGroups { get; set; }
public bool Hidden { get; set; }
public string ActivityType { get; set; }
public List<string> AnyGroupList { get; set; }
public List<string> AllGroupList { get; set; }
public GroupType GroupType { get; set; }
}
and here is the json of the group in the db
"Groups": [
{
"Id": "5ja34tefoq7sfj",
"Name": "new test",
"Slug": "new-test",
"Description": null,
"AdminOnly": true,
"NewsContribute": false,
"AutoGroup": false,
"LowerAgeLimit": 0,
"UpperAgeLimit": 0,
"Gender": null,
"Members": [],
"MemberNames": [],
"MemberDetails": [],
"RelatedCommunityId": null,
"CreatedBy": "Activity",
"SmartGroup": false,
"AndRules": null,
"OrRules": null,
"ParentGroup": null,
"EntryGroups": null,
"Hidden": false,
"ActivityType": null,
"AnyGroupList": null,
"AllGroupList": null,
"GroupType": "Default"
}
],
I have the following json:
{
"13377": {
"id": 13377,
"orderId": 13377,
"executionStatus": "-1",
"comment": "",
"htmlComment": "",
"cycleId": -1,
"cycleName": "Ad hoc",
"versionId": 10001,
"versionName": "Version2",
"projectId": 10000,
"createdBy": "vm_admin",
"modifiedBy": "vm_admin",
"assignedTo": "user1",
"assignedToDisplay": "user1",
"assignedToUserName": "user1",
"assigneeType": "assignee",
"issueId": 10013,
"issueKey": "SAM-14",
"summary": "Test",
"label": "",
"component": "",
"projectKey": "SAM",
"folderId": 233,
"folderName": "testfolder"
}
}
I create the following classes using json2csharp and get:
public class __invalid_type__13377
{
public int id { get; set; }
public int orderId { get; set; }
public string executionStatus { get; set; }
public string comment { get; set; }
public string htmlComment { get; set; }
public int cycleId { get; set; }
public string cycleName { get; set; }
public int versionId { get; set; }
public string versionName { get; set; }
public int projectId { get; set; }
public string createdBy { get; set; }
public string modifiedBy { get; set; }
public string assignedTo { get; set; }
public string assignedToDisplay { get; set; }
public string assignedToUserName { get; set; }
public string assigneeType { get; set; }
public int issueId { get; set; }
public string issueKey { get; set; }
public string summary { get; set; }
public string label { get; set; }
public string component { get; set; }
public string projectKey { get; set; }
public int folderId { get; set; }
public string folderName { get; set; }
}
public class RootObject
{
public __invalid_type__13377 __invalid_name__13377 { get; set; }
}
When I deserialize in C# I receive no error but I get null?
Not sure how to approach this..any suggestions would be welcome.
thankyou.
Your Json object can be deserialized with an Dictionary. On this case, you can build your DataObject like you are doing:
public class Foo
{
public int id { get; set; }
public int orderId { get; set; }
...
}
And Deserialize to
Dictionary<string, Foo>
If you are using Newtonsoft:
var data = JsonConvert.DeserializeObject<Dictionary<string,Foo>(json);
You can use sometimes, when the type of object is unknown, the dynamic keyword:
var data = JsonConvert.DeserializeObject<dynamic>(json);
There is a note in the developer road map from December of 2013 saying, "Lock/Unlock – We’ve added support for locking and unlocking files into the V2 API."
I've been all through the V2 API (for c#) and cannot find it anywhere. I expected to find something in the BoxFilesManager class or as something you would pass to UpdateInformationAsync within the BoxFileRequest class.
So is there a way to lock/unlock a file?
Great question. In order to see the current lock status of a file do a
GET https://api.box.com/2.0/files/7435988481/?fields=lock
If there is no lock on the file, you'll get something like this back:
{
"type": "file",
"id": "7435988481",
"etag": "0",
"lock": null
}
If you want to lock a file, you need to do a PUT (update) on the /files/ endpoint with a body that tells us what type of lock, and when to release it. Like this:
PUT https://api.box.com/2.0/files/7435988481/?fields=lock
{"lock": {
"expires_at" : "2014-05-29T19:03:04-07:00",
"is_download_prevented": true
}
}
You'll get a response confirming your lock was created:
{
"type": "file",
"id": "7435988481",
"etag": "1",
"lock": {
"type": "lock",
"id": "14516545",
"created_by": {
"type": "user",
"id": "13130406",
"name": "Peter Rexer gmail",
"login": "prexer#gmail.com"
},
"created_at": "2014-05-29T18:03:04-07:00",
"expires_at": "2014-05-29T19:03:04-07:00",
"is_download_prevented": true
}
}
Since there isn't a lock/unlock yet, I created a Lock Manager based on the existing managers:
class BoxCloudLockManager : BoxResourceManager
{
#region Lock/Unlock Classes
[DataContract]
internal class BoxLockRequestInfo
{
[DataMember(Name = "status")]
public string Status { get; set; }
//[DataMember(Name = "expires_at")]
//public string ExpiresAt { get; set; }
[DataMember(Name = "is_download_prevented")]
public bool IsDownloadPrevented { get; set; }
}
[DataContract]
internal class BoxLockRequest
{
[DataMember(Name = "lock")]
public BoxLockRequestInfo Lock { get; set; }
}
#endregion
const string LockFileString = "{0}/?fields=lock";
public BoxCloudLockManager(IBoxConfig config, IBoxService service, IBoxConverter converter, IAuthRepository auth)
: base(config, service, converter, auth)
{
}
public async Task<BoxLockInfo> LockAsync(string documentId,bool isDownloadPrevented = true)
{
var lockRequest = new BoxLockRequest { Lock = new BoxLockRequestInfo { Status = "lock", IsDownloadPrevented = isDownloadPrevented } };
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Put)
.Payload(_converter.Serialize(lockRequest));
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
public async Task<BoxLockInfo> UnlockAsync(string documentId)
{
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Put)
.Payload("{\"lock\":null}");
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
public async Task<BoxLockInfo> GetLockInfoAsync(string documentId)
{
BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(LockFileString, documentId))
.Method(RequestMethod.Get);
IBoxResponse<BoxLockInfo> response = await ToResponseAsync<BoxLockInfo>(request).ConfigureAwait(false);
return response.ResponseObject;
}
}
I derived a class from BoxClient, adding a LockManager and instantiate it within the Constructor.
Here is the Lock Info:
[DataContract]
public class BoxLockedBy
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "login")]
public string Login { get; set; }
}
[DataContract]
public class BoxLockDetails
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "created_by")]
public BoxLockedBy CreatedBy { get; set; }
[DataMember(Name = "created_at")]
public string CreatedAt { get; set; }
[DataMember(Name = "expires_at")]
public string ExpiresAt { get; set; }
[DataMember(Name = "is_download_prevented")]
public bool IsDownloadPrevented { get; set; }
}
[DataContract]
public class BoxLockInfo
{
[DataMember(Name = "type")]
public string Type { get; set; }
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "etag")]
public string Etag { get; set; }
[DataMember(Name = "lock")]
public BoxLockDetails LockDetails { get; set; }
}
I am receiving a FormatException when trying to load a an Album document from the default RavenDB database:
using (var session = _documentStore.OpenSession())
{
var album = session.Load<Album>(500);
//....
}
The Album JSON document in the database looks like this:
{
"AlbumArtUrl": "/Content/Images/placeholder.gif",
"Genre": {
"Id": "genres/10",
"Name": "Classical"
},
"Price": 8.99,
"Title": "The Best of Beethoven",
"CountSold": 0,
"Artist": {
"Id": "artists/203",
"Name": "Nicolaus Esterhazy Sinfonia"
}
}
And my in-memory entity Album class looks like this:
public class Album
{
public long Id { get; set; }
public string AlbumArtUrl { get; set; }
public DenomralizedGenre Genre { get; set; }
public decimal Price { get; set; }
public string Title { get; set; }
public int CountSold { get; set; }
public DenomralizedArtist Artist { get; set; }
}
public class DenomralizedGenre
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DenomralizedArtist
{
public int Id { get; set; }
public string Name { get; set; }
}
What am I doing wrong here?
Make all your Id strings. You have them as int and long. In RavenDB Id's are strings.
The Id as a string would be Album/24 in RavenDB. The Class name or type plus the HiLo value (created by the client tools) make up the Id.