-
Mar 6th, 2012, 02:22 PM
#1
Strongly typed json results from ajax call
In the success section of my ajax call I'm able to set variables based on the index of the raw json that's returned from my WCF RIA service like this:
Code:
var results = data.GetCorrespondenceByIDResult;
var correspondence = results.RootResults[0];
var contact = results.IncludedResults[0];
var letter = results.IncludedResults[1];
The problem I run into is that sometimes I have additional objects returned (The organization for the contact via a go between table called contactorganization) that throws off the index of the letter variable.
When looking at the results returned from the service I'm able to see that each of the items has is denoted as a stongly typed object in the form of
"__type":"Letter:#CRM.Data.Models"
Rather than get the results by index how can I look for the type and assign the variables based on that?
-
Mar 6th, 2012, 04:02 PM
#2
Re: Strongly typed json results from ajax call
If they are not JS arrays but are javascript objects - which you can access by "name" like this
var letter = results.IncludedResults["somename"];
I do things like this
Code:
if (typeof objInto["acs-reader-" + strSC] == "undefined") {
objInto["acs-reader-" + strSC] = {};
}
Do you have FIREBUG installed in the browser (I am assuming you use Firefox)??
-
Mar 6th, 2012, 04:55 PM
#3
Re: Strongly typed json results from ajax call
Here's my WCF RIA service query:
Code:
public Correspondence GetCorrespondenceByID(int correspondenceID)
{
return this.ObjectContext
.Correspondences
.Include("PhoneCalls")
.Include("InPersonVisits")
.Include("Letters")
.Include("Letters.Files")
.Include("Emails")
.Include("InvitationDetails2")
.Include("Contact")
.Include("Contact.ContactOrganizations.Organization")
.Where(c => c.ID == correspondenceID).FirstOrDefault();
}
All of the ugly includes are necessary when returning JSON but not when using the DomainDataSource which is weird.
Here's an example of the
{"GetCorrespondenceByIDResult":{"TotalCount":-1,"IncludedResults":[{"__type":"Contact:#CRM.Data.Models","CreatedBy":"user@domain.com","CreatedDate":"\/Date(1330989421240-0700)\/","FirstName":"Joe","ID":7062,"IsActive":true,"JobTitle":null,"LastName":"Smith","PeopleID":7757,"St ateAgencyID":null,"StateAgencyRoutingID":null},{"__type":"Letter:#CRM.Data.Models","CorrespondenceID ":114,"Details":"Some details about the incident","ID":6937},{"__type":"ContactOrganization:#CRM.Data.Models","ContactID":7062,"CreatedBy":" user@domain.com","CreatedDate":"\/Date(1330989427377-0700)\/","OrganizationID":558},{"__type":"Organization:#CRM.Data.Models","CreatedBy":"user@domain.com","Cre atedDate":"\/Date(1330989419727-0700)\/","IsActive":true,"OrganizationID":558,"OrganizationName":"Some organization name","StateAgencyID":null}],"RootResults":[{"ContactID":7062,"CreatedBy":"user@domain.com","CreatedDate":"\/Date(1328079600000-0700)\/","ID":114,"SituationID":4118}]}}
I'm not a master at JSON but it looks like both RootResults and IncludedResults are both arrays.
Firebug seems to confirm this saying GetCorrespondenceByIDResult is an object with TotalCount -1 IncludedResults[4] and RootResults[1]
-
Mar 6th, 2012, 05:04 PM
#4
Re: Strongly typed json results from ajax call
Last edited by szlamany; Mar 6th, 2012 at 05:12 PM.
-
Mar 7th, 2012, 09:45 AM
#5
Re: Strongly typed json results from ajax call
Originally Posted by szlamany
First - I like your use of the term "strongly typed" - json and javascript couldn't be further from that paradigm
Oops, was working with knockoutjs modeling on another project before posting the question.
Originally Posted by szlamany
Code:
var contact = "";
for (var i = 0; i < results.IncludedResults.length; i++) {
if (results.IncludedResults[i]["__type"] == "Contact:#CRM.Data.Models") {
contact = results.IncludedResults[i].LastName + ' ' + results.IncludedResults[i].FirstName;
// do whatever else with this array element
}
}
Ah, was missing ["__type"] when trying to loop through the results. Knew it was something simple.
Refactored it to:
Code:
function getIncludedResultsByType(results, type) {
for (var i = 0; i < results.IncludedResults.length; i++) {
if (results.IncludedResults[i]["__type"] === type) {
return results.IncludedResults[i];
}
}
}
So it can be called like:
Code:
var contact = getIncludedResultsByType(results, "Contact:#CRM.Data.Models");
var letter = getIncludedResultsByType(results, "Letter:#CRM.Data.Models");
-
Mar 7th, 2012, 09:53 AM
#6
Re: Strongly typed json results from ajax call
I didn't realize you wanted to "process" the "whole object"...
Nice function...
[btw] I have to remember to use the === operator instead of == ... to many languages too little time...[/btw]
-
Mar 7th, 2012, 10:12 AM
#7
Re: Strongly typed json results from ajax call
Yeah, I need the whole object. Now I've got a nice little function I can reuse though. Handy since I've got a couple hundred places in the application I can apply it.
Thanks.
Originally Posted by szlamany
too many languages too little time...
Tell me about it. Got a project coming where they require a Java and then another proof of concept one using Go.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|