I am attempting to add a row to a Class in my Parse database using the API
I have managed to get the row added but I noticed that both the 'ACL & user__user' fields are blank.
My Request class looks like this:
public class AccountDataRequest
{
public string OrderNo { get; set; }
public string SiteName { get; set; }
public string CreditDebit { get; set; }
public int Amount { get; set; }
public int CreditBalance { get; set; }
}
And my function looks like this:
public static AccountDataResponse AddTransaction(AccountDataRequest details)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.parse.com/1/classes/AccountData");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("X-Parse-Application-Id", "xxxxx");
httpWebRequest.Headers.Add("X-Parse-REST-API-Key", "xxxxx");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = JsonConvert.SerializeObject(details);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return JsonConvert.DeserializeObject<AccountDataResponse>(result);
}
}
}
The 'user__user' links to the 'objectID(String)' in mu Users class.
I have tried adding it to my class as a string but it throws and error.
Can anyone tell me how to get the ACL and 'user__user' populated please?
Thanks
UPDATE:
I have learnt that the Pointer is a structure so have amended my classes as follows:
public class AccountDataRequest
{
public string OrderNo { get; set; }
public string SiteName { get; set; }
public string CreditDebit { get; set; }
public double Amount { get; set; }
public int CreditBalance { get; set; }
public Pointer Pointer { get; set; }
}
public class Pointer
{
public string __type { get; set; }
public string className { get; set; }
public string objectId { get; set; }
}
So, when I now call the API with the following:
POST https://api.parse.com/1/classes/AccountData HTTP/1.1
Content-Type: application/json
X-Parse-Application-Id: xxxxx
X-Parse-REST-API-Key: xxxxx
Host: api.parse.com
Content-Length: 163
Expect: 100-continue
Connection: Keep-Alive
{"OrderNo":"9","SiteName":"Trev","CreditDebit":"Debit","Amount":1.0,"CreditBalance":999,"Pointer":{"__type":"Pointer","className":"AccountData","objectId":"fvJ8jPjyjx"}}
This call produces no errors and returns a createddate and an objectid but the user(Pointer <__User> is still null.
I also tried changing the className to "__User" to which the API responds:
{"code":111,"error":"invalid type for key Pointer, expected *AccountData, but got *_User"}
This is what the empty column looks like in my AccountData class
And I am trying to tie it to the objectId in my User table:
Can anyone see what is wrong please?
If the column in Parse is defined as a Pointer to _User, then you need to pass the appropriate structure to Parse and not just the objectId. Take a look at the Data Types section of the Parse REST SDK. It has a paragraph and sample for passing a Pointer structure through the API.
Similarly, for ACLs you need to specify that structure as well. Details for it are in the security docs for the SDK.
In both cases, if you want to use an object->JSON converter, you'll need to build an object that appropriately represents that structure. That'll be a bit easier for Pointers than ACLs as the former has predefined keys whereas the latter uses dynamic key names (e.g. objectIds and role names).
Updated to include class definition
public class AccountDataRequest
{
public string OrderNo { get; set; }
public string SiteName { get; set; }
public string CreditDebit { get; set; }
public double Amount { get; set; }
public int CreditBalance { get; set; }
public Pointer user { get; set; }
}
Related
I want to able to select certain entity properties (columns from db) in the include statement of queryable object. My query looks like below but I m getting error Lambda expression used inside Include is not valid
var samuraiWithQuotesQueryable = _context.Samurais.AsQueryable()
.Include(s => s.Quotes.Select(x => new { x.Text }));
// additional filters followed by getting the list
var samuraiList = samuraiWithQuotesQueryable.ToList();
Samurai and Quote entities look like below
public class Samurai
{
public Samurai()
{
Quotes = new List<Quote>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<Quote> Quotes { get; set; }
}
public class Quote
{
public int Id { get; set; }
public string Text { get; set; }
public Samurai Samurai { get; set; }
public int SamuraiId { get; set; }
}
Wondering if this is possible with the IQueryable object?
I have a slickgrid and am attempting to save its data back to the server.
When I breakpoint on the server, I can see the data in the Request.Form object, but I can't make it work with my object.
My data looks like...
[
{"id":"0","LineNumber":"","Detail":"MOT cost","Code":" ","Qty":"1","Est":" ","CustomerDamage":false,"Cost":"44.00","Value":"44.00","VAT":true,"SelfBillingLine":"False","DefectStatus":" "},
{"id":"62","LineNumber":"","Detail":"CRACKS IN Chassis","Code":"TLMA02","Qty":"1","Est":"","CustomerDamage":false,"Cost":"35.00","Value":"35.00","VAT":true,"SelfBillingLine":"False","DefectStatus":"Large repair"},
{"id":"63","LineNumber":"","Detail":"TEAR IN N/S CURTAIN","Code":"TLMA02","Qty":"1","Est":"","CustomerDamage":true,"Cost":"10.00","Value":"10.00","VAT":true,"SelfBillingLine":"False","DefectStatus":"Customer"}
]
I am posting with a button onclick...
$("#SBSave").click(function() {
debugger;
var details = JSON.stringify(defectrows);
save('SBDetail/SaveSBItem', details);
});
I have tried a number of things to receive the data, none of them work.
My controller...
[HttpPost]
public void SaveSBItem(SelfBillDetailList details, string Approve = "")
{
// Actions here.
}
My model...
Trying a number of things, neither work...
public class SelfBillDetailList
{
public IEnumerable<SelfBillingIncomingDetail> IncomingDetails { get; set; }
}
public class SelfBillingIncomingDetail
{
public int id { get; set; }
public string Code { get; set; }
public string LineNumber { get; set; }
public string Detail { get; set; }
public string Action { get; set; }
public string Qty { get; set; }
public string Est { get; set; }
public bool VAT { get; set; }
public bool CustomerDamage { get; set; }
public string Cost { get; set; }
public string Value { get; set; }
public DateTime Received { get; set; }
public string DefectStatus { get; set; }
public bool SelfBillingLine { get; set; }
}
So, I have tried an individual SelfBillingIncomingDetail and also a the SelfBillDetailList.
Neither work.
I have even sent an individual row, again, neither work.
I want to send it as a group, so it will be an array of SelfBillingIncomingDetail but nothing works.
Thank you for your help.
I have done it again... eventually found an answer after looking for ages.
Darin Dimitrov's answer in
Post an Array of Objects via JSON to ASP.Net MVC3
let me to the answer.
It seems that when sending the data, I need to give the array of data the same name as the property name in SelfBillDetailList, so...
var details = JSON.stringify({IncomingDetails : defectrows});
fixes it.
how to find latitude and longitude of a address in Wp7
ex :
If I give the location name : "New York" then i want get its latitude and longitude.
thanks in advance
This should work:
Class for deserializing the json string
public class PlaceInfo
{
public string place_id { get; set; }
public string licence { get; set; }
public string osm_type { get; set; }
public string osm_id { get; set; }
public List<string> boundingbox { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string display_name { get; set; }
public string #class { get; set; }
public string type { get; set; }
public double importance { get; set; }
public string icon { get; set; }
}
This is the code to get the informations from the website:
Format is JSON, i'm using the json serializor of c#
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
WebClient webClient = new WebClient();
string jsonString = webClient.DownloadString("http://nominatim.openstreetmap.org/search?city=%22new%20york%22&format=json");
//load into memorystream
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
{
//parse
var ser = new DataContractJsonSerializer(typeof(PlaceInfo[]));
PlaceInfo[] obj = (PlaceInfo[])ser.ReadObject(ms);
}
the array obj has now all places which where found with that name. for example jsut take the first place which was found obj[0].lon and obj[0].lat
WebClient webClient = new WebClient();
string xml = webClient.DownloadString("http://nominatim.openstreetmap.org/search?city=%22new%20york%22&format=xml");
maybe you can use openstreetmaps:
http://nominatim.openstreetmap.org/search?city=%22new%20york%22&format=json
http://nominatim.openstreetmap.org/search?city="---cityname---"&countrycodes="---CountryCode---"&limit=2&format=json
http://wiki.openstreetmap.org/wiki/Nominatim
you can get the result as json or xml
I'm using Json.Net to handle the deserialzing of the response of API calls from the Pipl.com API in my application, and it works fine but for some strange reason it won't deserialize a specific property of the JSON string that I feed to the JsonConvert.DeserializeObject method.
My class is this:
public class Source
{
public string Dsname { get; set; }
public bool IsSponsored { get; set; }
public string Url { get; set; }
public string Domain { get; set; }
public uint ExternalID { get; set; }
public Source()
{
}
}
and everything except the Dsname gets deserialized properly. The Json to be converted is like this:
"source": {
"#is_sponsored": false,
"#ds_name": "Personal Web Space -MySpace",
"url": "http://www.foo.bar"
"domain": "myspace.com"
}
Any idea how to go about this problem? Thank you in advance.
I added a wrapper class and specified the property name as attributes, like this:
public class Source
{
[JsonProperty(PropertyName = "#ds_name")]
public string Dsname { get; set; }
[JsonProperty(PropertyName = "#is_sponsored")]
public bool IsSponsored { get; set; }
public string Url { get; set; }
public string Domain { get; set; }
public uint ExternalID { get; set; }
}
public class RootObject
{
public Source source { get; set; }
}
Then I was able to deserialize fine:
var json = "{\"source\": { \"#is_sponsored\": true, \"#ds_name\": \"Personal Web Space -MySpace\", \"url\": \"http://www.foo.bar\", \"domain\": \"myspace.com\"}}";
var des = JsonConvert.DeserializeObject<RootObject>(json);
Note that I:
- wrapped your sample in a curly braces to make it valid JSON
- added a missing comma
- changed the value of "#is_sponsored" to not be the default value to verify it is desearialized correctly.
Ok I realize, this is a pretty old thread. But I ran into a similar issue earlier and came across this thread.
In my case the class I was trying to se/deserialize had a List<ClassName> public property in it. Which serialized fine, but wont deserialize. I switched it to ClassName[] and the fixed the deserialization problem.
Hope it helps someone else who comes across this thread, or at least gives them something else to look for.
Say I have the following:
(--> = 1 to many implemented as a collection in EF code-first)
Message --> UserMessage
Message --> Attachments
When I call the following:
var res = _ctx.DataContext.UserMessage.Where(x => x.UserId)
.Select(m => m.Message).ToList();
EDIT: added classes:
public class Message
{
public int MessageId { get; set; }
public ICollection<Attachment> Attachments { get; set; }
[Required]
public string Text { get; set; }
}
public class Attachment
{
public int AttachmentId { get; set; }
public int MessageId { get; set; }
public virtual Message Message { get; set; }
public string FileServerPath { get; set; }
}
public class UserMessage
{
public int UserMessageId { get; set; }
[Required]
public int MessageId { get; set; }
public Message Message { get; set; }
[Required]
public int UserId { get; set; }
public User User { get; set; }
}
I would expect the res variable to hold all the attachments but it is empty even if there are rows. What am I missing?
Your where condition doesn't make sense, I don't even think it compiles.
You say, you expect res to hold all attachments. Why should it? You don't even use Attachment anywhere in your query.
Without your actual classes, it is a bit hard to suggest the correct way, but I think it would be something like this:
var res = _ctx.DataContext.UserMessage.Where(x => x.UserId == currentUserId)
.SelectMany(m => m.Message.Attachments)
.ToList();
Now, res contains all attachments of all messages of the user with the id currentUserId.
I assumed a class layout like this:
class UserMessage
{
public int UserId {get;set;}
public Message Message {get;set;}
}
class Message
{
public IEnumerable<Attachment> Attachments {get;set;}
// Irrelevant for the query in its current form:
public IEnumerable<UserMessage> UserMessages {get;set;}
}
On the context it needs to be told to acquire the navigation properties with an include such as
_ctx.UserMessage.Include("Attachments")
.SelectMany( ... )
HTH