Why does assigning a JArray to a var fail when I call JsonConvert.DeserializeObject()? - json.net

On this line of code:
var arr = JsonConvert.DeserializeObject<JArray>(s);
...I am getting, "Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'."
I changed that line to this:
JArray arr = JsonConvert.DeserializeObject<JArray>(s);
...and got the same err msg.
I changed it to this:
var arr = JsonConvert.DeserializeObject<JObject>(s);
...and it wouldn't even compile.
The value of what has been read by the call (in string s) at this point is:
{"id":347745,"results":[{"iso_3166_1":"US","release_dates":[{"certification":"","iso_639_1":"","note":"","release_date":"1936-12-12T00:00:00.000Z","type":3}]}]}
All I want from it is the value for "certification"; In this case, the certification value is an empty string ("certification":"")
In context, the code is:
. . .
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(RESTStringToGetMPAARatingForMovieId);
webRequest.Method = "GET";
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
{
StreamReader streamReader = new StreamReader(webResponse.GetResponseStream());
string s = streamReader.ReadToEnd();
var arr = JsonConvert.DeserializeObject<JArray>(s);
//JArray arr = JsonConvert.DeserializeObject<JArray>(s);
//var arr = JsonConvert.DeserializeObject<JObject>(s);
foreach (JObject obj in arr)
{
_currentMPAARating = (string)obj["certification"];
. . .
}
}
else
{
MessageBox.Show(string.Format("Status code == {0}, Content length == {1}",
webResponse.StatusCode, webResponse.ContentLength));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Your JSON is not an array, it is an object which contains an array (results). But it's actually more complicated than that: the certification string you seek is nested even further down inside a second release_dates array.
If you take your JSON and reformat it using a JSON validator/beautifier, it should become more clear:
{
"id": 347745,
"results": [
{
"iso_3166_1": "US",
"release_dates": [
{
"certification": "",
"iso_639_1": "",
"note": "",
"release_date": "1936-12-12T00:00:00Z",
"type": 3
}
]
}
]
}
So to get the data you are looking for using regular foreach loops, you would need code like this:
var obj = JsonConvert.DeserializeObject<JObject>(s);
var resultArr = (JArray)obj["results"];
foreach (JObject resultObj in resultArr)
{
var releaseDatesArr = (JArray)resultObj["release_dates"];
foreach (JObject releaseDateObj in releaseDatesArr)
{
_currentMPAARating = (string)releaseDateObj["certification"];
// ...
}
}
Fiddle: https://dotnetfiddle.net/SMzQTw
If all you need is the one item, here's a shortcut. Use the SelectToken method with the recursive descent operator (..) like this:
var obj = JsonConvert.DeserializeObject<JObject>(s);
_currentMPAARating = (string)obj.SelectToken("..certification");
Fiddle: https://dotnetfiddle.net/S1ScLO
But note the above will only return the first match. If you are expecting multiple certifications, you can use SelectTokens (plural) instead:
var obj = JsonConvert.DeserializeObject<JObject>(s);
var ratings = obj.SelectTokens("..certification").Select(t => (string)t).ToList();
Fiddle: https://dotnetfiddle.net/zyjNnJ

Related

how we can return a status code for the serialized JSON object using Newtonsoft.net

I have this Action method which act as an API end point inside our ASP.NET MVC-5, where it search for a username and return the username Phone number and Department from Active Directory (we are serializing the object using Newtonsoft.net):-
public ActionResult UsersInfo2()
{
DomainContext result = new DomainContext();
try
{
// create LDAP connection object
DirectoryEntry myLdapConnection = createDirectoryEntry();
string ADServerName = System.Web.Configuration.WebConfigurationManager.AppSettings["ADServerName"];
string ADusername = System.Web.Configuration.WebConfigurationManager.AppSettings["ADUserName"];
string ADpassword = System.Web.Configuration.WebConfigurationManager.AppSettings["ADPassword"];
using (var context = new DirectoryEntry("LDAP://mydomain.com:389/DC=mydomain,DC=com", ADusername, ADpassword))
using (var search = new DirectorySearcher(context))
{
// create search object which operates on LDAP connection object
// and set search object to only find the user specified
// DirectorySearcher search = new DirectorySearcher(myLdapConnection);
// search.PropertiesToLoad.Add("telephoneNumber");
search.Filter = "(&(objectClass=user)(sAMAccountName=test.test))";
SearchResult r = search.FindOne();
ResultPropertyCollection fields = r.Properties;
foreach (String ldapField in fields.PropertyNames)
{
// cycle through objects in each field e.g. group membership
// (for many fields there will only be one object such as name)
string temp;
// foreach (Object myCollection in fields[ldapField])
// {
// temp = String.Format("{0,-20} : {1}",
// ldapField, myCollection.ToString());
if (ldapField.ToLower() == "telephonenumber")
{
foreach (Object myCollection in fields[ldapField])
{
result.Telephone = myCollection.ToString();
}
}
else if (ldapField.ToLower() == "department")
{
foreach (Object myCollection in fields[ldapField])
{
result.Department = myCollection.ToString();
}
}
// }
}
string output = JsonConvert.SerializeObject(result);
return Json(output,JsonRequestBehavior.AllowGet);
}
}
catch (Exception e)
{
Console.WriteLine("Exception caught:\n\n" + e.ToString());
}
return View(result);
}
now the return JSON will be as follow:-
"\"DisplayName\":null,\"Telephone\":\"123123\",\"Department\":\"IT\",\"Name\":null,\"SamAccountName\":null,\"DistinguishedName\":null,\"UserPrincipalName\":null}"
but in our case we need to return a status code beside the return json data. for example inccase there is an exception we need to return an error code,also if we are able to get the user's info we need to pass succes code 200, and so on.. so how we can achieve this?
you can try something like this
var statusCode=200;
string output = JsonConvert.SerializeObject( new { result = result, StatusCode = statusCode);
but nobody usually do this. When users call API they can check status code that HTTP Client returns, using code like this
var response = await client.GetAsync(api);
//or
var response = await client.PutAsJsonAsync(api, data);
var statusCode = response.StatusCode.ToString();
//or usually
if (response.IsSuccessStatusCode) {...}
else {...}

Checking for multiple postalAddresses for the contact swift 5

I finally got time to go back and work on this. The code below pulls the address labels and IDs. The IDs are fine. The problem is when I print theAddressLabel it give me this:
_$!<Home>!$_
Is this normal or is there something in my code I can change to fix it? All I want it to contain is "Home". Do I need to strip-out the first and last four characters before it goes into the array?
func getContactFromID_Ouote2(contactID: String)
{
struct contactAddresses
{
var theLabel: String
var theID: String
}
let store = CNContactStore()
var theName = CNContact()
var theTypes = [contactAddresses]()
var theAddressID: String = ""
var theAddressLabel: String = ""
let theKeys = [CNContactPostalAddressesKey] as [CNKeyDescriptor]
do {
theName = try store.unifiedContact(withIdentifier: contactID, keysToFetch: theKeys)
let postalAddress = theName.postalAddresses
postalAddress.forEach { (mailAddress) in
theTypes.append(contactAddresses(theLabel: mailAddress.label!, theID: mailAddress.identifier))
}
for theItem in theTypes
{
theAddressLabel = theItem.theLabel
theAddressID = theItem.theID
print(theAddressLabel)
print(theAddressID)
}
} catch {
print("Fetching contact data failed: \(error)")
}
How do I check to see if there are multiple postalAddresses (home, work, etc.) for a contact?
If there are multiple postalAddresses then my plan is to present an alert to allow the user to pick the one to use. Presenting the alert isn't a problem, I just need help with getting the addresses.
Thanks in advance.
func getContactFromID_Ouote(contactID: String)
{
let store = CNContactStore()
var theName = CNContact()
let theKeys = [CNContactNamePrefixKey,
CNContactGivenNameKey,
CNContactFamilyNameKey,
CNContactNameSuffixKey,
CNContactOrganizationNameKey,
CNContactPostalAddressesKey,
CNContactFormatter.descriptorForRequiredKeys(for: .fullName)] as! [CNKeyDescriptor]
do {
theName = try store.unifiedContact(withIdentifier: contactID, keysToFetch: theKeys)
contactName = CNContactFormatter.string(from: theName, style: .fullName)!
contactPrefix = theName.namePrefix
contactFirst = theName.givenName
contactLast = theName.familyName
companyName = theName.organizationName == "" ? "" : theName.organizationName
} catch {
print("Fetching contact data failed: \(error)")
}
if let firstPostalAddress = (theName.postalAddresses.first),
let labelValuePair = firstPostalAddress.value(forKey: "labelValuePair") as? AnyObject,
let finalPostalAddress = labelValuePair.value(forKey: "value") as? CNPostalAddress
{
mailAddress = CNPostalAddressFormatter.string(from: finalPostalAddress, style: .mailingAddress)
}
}
Your can use below code for fetch multiple mailingAddresses.
func getContactFromID_Ouote(contactID: String)
{
let store = CNContactStore()
var theName = CNContact()
let theKeys = [CNContactEmailAddressesKey] as [CNKeyDescriptor]
do {
theName = try store.unifiedContact(withIdentifier: contactID, keysToFetch: theKeys)
let emailAddress = theName.emailAddresses
emailAddress.forEach { (mailAddress) in
print("Your Mail Address is :- ",mailAddress.value)
print("Your Mail Type :- ",mailAddress.label)
}
} catch {
print("Fetching contact data failed: \(error)")
}
}

Getting error when a method is made for post request

When made I post request is made its giving internal server. Is the implementation of Flurl is fine or I am doing something wrong.
try
{
Models.PaymentPost paymentPost = new Models.PaymentPost();
paymentPost.Parts = new Models.Parts();
paymentPost.Parts.Specification = new Models.Specification();
paymentPost.Parts.Specification.CharacteristicsValue = new List<Models.CharacteristicsValue>();
paymentPost.Parts.Specification.CharacteristicsValue.Add(new Models.CharacteristicsValue { CharacteristicName = "Amount", Value = amount });
paymentPost.Parts.Specification.CharacteristicsValue.Add(new Models.CharacteristicsValue { CharacteristicName = "AccountReference", Value = accountId });
foreach (var item in extraParameters)
{
paymentPost.Parts.Specification.CharacteristicsValue.Add(new Models.CharacteristicsValue {
CharacteristicName = item.Key, Value = item.Value });
}
var paymentInJson = JsonConvert.SerializeObject(paymentPost);
var selfCareUrl = "http://svdt5kubmas01.safari/auth/processPaymentAPI/v1/processPayment";
var fUrl = new Flurl.Url(selfCareUrl);
fUrl.WithBasicAuth("***", "********");
fUrl.WithHeader("X-Source-System", "POS");
fUrl.WithHeader("X-Route-ID", "STKPush");
fUrl.WithHeader("Content-Type", "application/json");
fUrl.WithHeader("X-Correlation-ConversationID", "87646eaa-2605-405e-967c-56e8002b5");
fUrl.WithHeader("X-Route-Timestamp", "150935");
fUrl.WithHeader("X-Source-Operator", " ");
var response = await clientFactory.Get(fUrl).Request().PostJsonAsync(paymentInJson).ReceiveJson<IEnumerable<IF.Models.PaymentPost>>();
return response;
}
catch (FlurlHttpException ex)
{
dynamic d = ex.GetResponseJsonAsync();
//string s = ex.GetResponseStringAsync();
return d;
}
You don't need to do this:
var paymentInJson = JsonConvert.SerializeObject(paymentPost);
PostJsonAsync just takes a regular object and serializes it to JSON for you. Here you're effectively double-serializing it and the server is probably confused by that format.
You're also doing a lot of other things that Flurl can do for you, such as creating those Url and client objects explicitly. Although that's not causing errors, this is how Flurl is typically used:
var response = await selfCareUrl
.WithBasicAuth(...)
.WithHeader(...)
...
.PostJsonAsync(paymentPost)
.ReceiveJson<List<IF.Models.PaymentPost>>();

How do I iterate over all the rows in my sheet to find match?

I am creating a script with google appscript to
read files in a folder
parse the name of the file in the folder
check if that name already exists in the sheet
if not add that to the list
if it does exist then check another column to see if an email is sent
if yes do nothing if no send the email.
I have tried
index of
,for loop to iterate over range.getValues()
None of them work properly as expected.
The data is of length 3.
function myFunction() {
getFileNameFromFolders('1TcR5oUKwH9hUG9xHBA6HuXQOr40etS5z');
}
function getFileNameFromFolders(folderID) {
var folder = DriveApp.getFolderById(folderID);
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var fileName = file.getName();
var agentDetails = fileName.split("-");
var agentID = agentDetails[0];
var fileType = agentDetails[1];
var fileUrl = file.getUrl();
var fileDate = agentDetails[2];
locateAgent(agentID, fileType, fileDate, fileUrl, fileName);
}
}
function locateAgent(agentID, fileType, fileDate, url, uniqueKey) {
Logger.log('locating ' + uniqueKey);
var spreadSheet = SpreadsheetApp.openByUrl(SpreadsheetApp.getActiveSpreadsheet().getUrl());
var sheet = spreadSheet.getSheets()[0];
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, 6)
var data = range.getValues();
for (var i in data) {
if (data[i][5] == uniqueKey) {
Logger.log('yes');
break;
}
else { Logger.log('no');
var newRange = sheet.appendRow([agentID,fileType, fileDate, url, 'r', uniqueKey]);}
}
}
function sendEmails(email, fileUrl) {
var asPDF = DriveApp.getFileById(getIdFromUrl(fileUrl));
MailApp.sendEmail(email, 'test-email-with-agent-stuff-thing-i-dont-know-the-name', 'you should recieve a file named AID-2 as you are registered as 2', {
name: 'Automatic Emailer Script from DOER',
attachments: asPDF.getAs(MimeType.PDF)
});
}
function getIdFromUrl(url) {
return url.match(/[-\w]{25,}$/);
}
The loop adds to the list even though it exists. I may be getting the concept. If you have any other way I can do this, I would highly appreciate it.
You can implement a boolean variable which will be set to true if uniqueKey exists alredy
Modify your code as following:
function locateAgent(agentID, fileType, fileDate, url, uniqueKey) {
Logger.log('locating ' + uniqueKey);
var spreadSheet = SpreadsheetApp.openByUrl(SpreadsheetApp.getActiveSpreadsheet().getUrl());
var sheet = spreadSheet.getSheets()[0];
var range = sheet.getRange(2, 1, sheet.getLastRow()-1, 6)
var data = range.getValues();
var exists=false;
for (var i in data) {
if (data[i][5] == uniqueKey){
exists=true;
var row=i;
break;
}
}
if(exists==false){
Logger.log('it does not exist yet');
var insertRange = sheet.getRange(sheet.getLastRow()+1, 1, 1, 6);
//adapt according to your needs:
insertRange.setValues([[agentID],[fileType],[fileDate],[url],[],[uniqueKey]]);
}else{
//implement here your statement to check status column, e.g.:
if(data[row][status column]!="Sent"){
sendEmails(...);
}
}
}

Modify BuildApiResponse in ASP.Net Web Api

I am new in ASP.NET MVC Web API. I am trying to modified the return JSon to this format
{
"Error": false,
"Status": 200,
"Response": []
}
Now I able to do that by follow this post https://www.devtrends.co.uk/blog/wrapping-asp.net-web-api-responses-for-consistency-and-to-provide-additional-information . But the problem is I not able to show ModelState error like 'First name is required' because the code only show the first hit error.
if (error != null)
{
content = null;
//only show the first error
errorMessage = error.Message;
}
So I did some modification, now the code is written as below:
if (error != null)
{
content = null;
foreach(var e in error)
{
//if the error's type is ModelState
if (e.Key.Equals("ModelState"))
{
var allErrors = e.Value;
foreach (var modelError in (IEnumerable<KeyValuePair<string, object>>)allErrors)
{
var msg = modelError;
errorMessage = string.Concat(errorMessage, ", ", ((String[]) modelError.Value)[0]);
}
}
else
{
errorMessage = e.Value.ToString();
}
}
}
Now it's able to show all errors but the code is messy. I am writing this questions to find out what is the proper way to write this kind of code.
You can iterate over all the errors and concatenate them using StringBuilder. String.Join is much faster than Append for less than 1000 items (it is unlikely you will have so many errors in the modelstate object):
public static ValidationResult CheckValid(ModelStateDictionary modelState, string httpName = null)
{
if (!modelState.IsValid)
{
var sb = new StringBuilder();
sb.AppendLine(httpName + " failed: Invalid Json:");
foreach (var pair in modelState)
{
var error = String.Join(";", pair.Value.Errors.Select
(
i =>
{
if (!String.IsNullOrEmpty(i.ErrorMessage))
return i.ErrorMessage;
return i.Exception.Message;
}
));
sb.AppendLine($"Property: {pair.Key} Errors: ({error})");
}
return new ValidationResult(false, sb.ToString());
}
else
return new ValidationResult(true, "");
}

Resources