How to divide full name text from textfield into two strings(firstname and secondname) in swift - json

I have one textfield called fullnameTextField here i am writing firstname space secondname in textfield
while parsing i need firstname separately and secondname separately,
how to add firstname and secondname parameters from one fullnameTextField
#IBOutlet weak var fullName : UITextField!
and my api parameters are
let param = ["fname" : "hello",
"lname": "test",
"country_code": "1",
"password" : txtPassword.text?.trim() ?? "",
"password_confirmation" : txtConfirmPassword.text?.trim() ?? "",
"phone" : mobileNum
]
how to add one input to two parameters

There is no solution for this
If You need 2 fields for any other services(Stripe, etc) only way is use 2 inputs
We have too many variables of full names. It can contain 2 - 5 words or like utf16 symbols string (japanese, arabic).
Also Full name field not forcing to write first name in first, so even if u have 2 basic words, after separating it by whitespace u can be sure which one this parts is

Related

how to convert json string back to an object in Angular 8

I converted this object into JSON string.
values of subject1, subject2, subject 3 are coming from input fields in angular frontend
subject = {
"subject1": "A",
"subject2": "B",
"subject3": "C"
}
and now I have a JSON string like this,
const subjects = {"subject1": "A", "subject2": "B","subject3":"c"}
Like this, I saved this as a JSON string in DB. In another place, I wanted to access this string as an object as previously I made.
to output these subjects separately as same as It gets as an object.
example:- First input box - subject1 value as A , Second input box - subject2 value as B
How can I put them back again like separate values into separate variables or any other way to separate them and put back into the text fields?
I just tried to get that JSON string and tried to access subject1 like
subject1 = subjects.subject1 like that I can put subject1 in relevant text field.
But that doesn't work. I checked previous questions like this. But they didn't answer my question. How can I solve this?
You can do it with JSON.parse.
const obj = JSON.parse(subjects);
console.log(obj.subject1); // "A"
You can use destructuring assignment:
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
const subject = {
"subject1": "A",
"subject2": "B",
"subject3": "C"
}
const {subject1,subject2,subject3} = subject;
console.log(subject1);
console.log(subject2);
console.log(subject3);
You can find more about destructuring assignment here

Spring data jpa and json query

Got a json request body as follow:
{
"firstName": "John",
"lastName": "Doe",
"phoneNumber": "0123456789"
}
I want to search the related entry in MongoDB, even if a field is missing, in which case, the missing field must match any values.
For example, the following query will search all entries having a matching firstName:
{
"firstName": "John"
}
While the previous JSON will have to match all the 3 parameters. If one of the 3 value is blank, then it should include blank in the search.
But if a key-value pair is missing, like in the second JSON, it has to match any value for that key.
Questions
Should I write a finder for all combinations?
E.g. something like:
#Repository
List<Phonebook.Entry> findAllEntries();
#Query("{firstName : ?0, lastName: ?1}")
List<Phonebook.Entry> findEntryByName(String firstName, String lastName);
#Query("{firstName : ?0}")
List<Phonebook.Entry> findEntryByFirstName(String firstName);
#Query("{lastName : ?0}")
List<Phonebook.Entry> findEntryByLastName(String lastName);
#Query("{phoneNumber : ?0}")
List<Phonebook.Entry> findEntryByPhoneNumber(String phoneNumber);
#Query("{firstName : ?0, phoneNumber : ?1}")
List<Phonebook.Entry> findEntryByFirstNameAndPhoneNumber(String firstName, String phoneNumber);
#Query("{lastName : ?0, phoneNumber : ?1}")
List<Phonebook.Entry> findEntryByLastNameAndPhoneNumber(String lastName, String phoneNumber);
#Query("{firstName : ?0, lastName : ?1, phoneNumber : ?2}")
List<Phonebook.Entry> findEntryByLastNameAndFirstNameAndPhoneNumber(String firstName, String lastName,String phoneNumber);
}
How do I transform the query to do a "like" search instead?
For example, the following JSON would return all the entry with the first name starting with "Jo" instead of the exact value as in question 1.
{
"firstName": "Jo"
}
Thanks!
Definitly don't do that. Combining params like that is always bad idea.
Take a look here in 3.4 section how to simulate LIKE. Your method should look something like findByFirstNameLikeOrLastNameLikeOrPhoneNumberLike(String firstName, String lastName, String phoneNumber) . When passing arguments to this method, you should pass empty String instead of null. Maybe this method is not really good from performance side.
Also, take a look at findByExample method described here .

How to aggregate user logs based on email domain name in ElasticSearch

I have a Json field called UserId in each document. I would like to create a new field that contains only the email domain.
Example:
{
"FirstName" : "John",
"LastName" : "Smith",
"Email" : "jsmith#gmail.com"
}
How do I create an analyzer and a copy_to field that will let me have an additional field: EmailDomain : "gmail.com"
I know that the tokenizer will be the # char, but I don't know how to create the analyzer, etc to do the rest of this.
Thank you!

Getting and displaying JSON data fields using HTML and AngularJS

Im new to angularJS and web designing as a whole. Im trying to get a data field(or element) from a JSON. For example, this is what the JSON looks like
{
"Name":"Raymond Eugene Monce",
"Dateofbirth":"1924-0308T00:00:00Z",
"Ethnicity":"Caucasian",
"Languages":["{English}"],
},
and I'm trying to get the "Name" data field. This is what my .js file looks like,
var profile = angular.module('profile', ['ui.bootstrap','ngResource']);
profile.controller("profileController", ["$scope","$resource", function($scope, $resource) {
// get the user id
$scope.userid = sessionStorage["cerestiuserid"];
// json we get from server
$scope.apicall = sessionStorage["cerestihome"]; // NEED TO CHANGE API
// grabs the user we want
$scope.userResource = $resource($scope.apicall + "/api/userprofile/",
{Userid:21},
{'get':{method: 'POST'}}
);
// fetch JSON
$scope.userResource.get(function(result) {
// get the name field
$scope.name = result;
sessionStorage["name"] = JSON.stringify(result);
});
and my .html file,
<div ng-controller = "profileController" style="float:left">
<!-- profile pic -->
<div class="pull-left">
<div class="container-fluid">
<div class="profile">
<div class="row">
<div class="center-block">
<div class="profilePic">
<img ng-src="{{profilePic()}}" class="img-responsive">
<!-- name field -->
<label class="caption">
<h4>{{name.name}}</h4>
</label>
</div>
Again, Im not having problems with the Database or API calls. I just want to know how I can get and display the name field of the JSON. Thanks.
strelok2010's comment above should work although that depends on if your result really looks like the one defined at the top of your question.
Your result seems to be a normal javascript object not JSON. (yeah they are different, and that confused me when I learned it.) I assume that because you stringify the result from a javascript object into JSON. Therefore if that is working right your result is either a javascript object or an array of javascript objects. I'm assuming an array. You might want to check though.
I noticed your earlier post had a related problem.
In that one you were asking to access a property of an object that was in an array. In that case it was result as well. Here was the answer from your previous question
var result = [{"name": "Jason"
"date of birth": "february 23, 2985"
....
}];
var firstResultsName = result[0].name;
There are two things I am unsure of due to the inconsistency between this and your last question.
First your name property in your results object is spelled with a capital N here as opposed to a lower case n in your last question.
Keep in mind that capitilization matters in javascript.
Second your result in your last question was an array of objects and in this it seems to be just an object.
So depending on which one it is will determine your solution. So instead of writing every possible solution I'll show you how to determine the solution.
Remember we are dealing with a normal array of javascript objects. I'll try to go into detail so it's extra clear (sorry I heard you were new to web developement, I'm assuming JavaScript too.), but sorry if it's a little too detailed. I will also be breaking it into parts to go deeper into the array of objects that I'll use in my example, but traversing into the data structure can all be done in a single line as I will show.
You can only do actions on the 'outermost-form' (by the way 'outermost-form' is just a term I'll use for clarification it's not really a technical term.) and work your way into the collection (object/array/string)
As an example we have an array of people, with the array being the 'outermost-form'
var people = [
{
"name": "Bob",
"occupation": "Architect",
"date of birth": "01/23/83"
},
{
"name": "Timothy",
"Occupation": "Accountant",
"date of birth": "02/23/78"
}
];
If we saw the value of people at this moment it not surprisingly be.
[
{
"name": "Bob",
"occupation": "Architect",
"date of birth": "01/23/83"
},
{
"name": "Timothy",
"Occupation": "Accountant",
"date of birth": "02/23/78"
}
]
Start with the Array
Since it's an array as the 'outermost-form' we can get one of its values using an index. Just like any other array. Just for a bit of contrast I'll show you how what we are doing is similar to any other array by showing an example of an array by itself
// simple array example
var array = ["foo", "bar", "baz"];
array[0] // returns "foo"
// more simple array example, but less practical (it's more just for showing how javascript can work.)
["foo", "bar", "baz"][2] // returns "baz"
Back to our main example. Let's make a variable person and store our first person in the people array in that value.
var person = people[0];
Now if saw our person variable it would equal the following
{
"name": "Bob",
"occupation": "Architect",
"date of birth": "01/23/83"
}
You can see just like the normal array it grabs the first item in the array. You can see how we are slowly traversing into our people data structure. (that being an array of objects.)
Enter the Object
Okay so now we have the person object, but we want the name of that person so since we are dealing with an object we have to access its properties we can do this with either 'dot notation', e.g. <object>.<property>, or 'bracket notation' which can be done with either a variable or a string for the property name. e.g. <object>.["<property>"] or <object>.[<variable>]
So just as a side example I will show you what it normally takes to get the value of a property of an object just so you can compare and see there's no 'magic' going on. Keep in mind javascript is case-sensitive. Also javascript objects properties can go with or without surrounding quotes unlike JSON. One last thing having a space in the property name forces us to use quotes, and also forces us to access that property via bracket notation.
var result;
var obj = { foo: 1, Bar: 2, "foo bar": 3 };
var randomVarName = "Bar"; // notice the capital B in Bar is important since it was declared that way.
result = obj.foo; // result equals 1
result = obj[randomVarName]; // result equals 2
result = obj["foo bar"]; // result equals 3
Back again to our main train of thought. So we have traversed into our people array to find the person object now let's get their name.
var name = person.name;
The value of name would be.
"Bob"
You can do with that what you wish. You could have also used any of the previous ways to get an objects property including bracket notation.
Do Everything we just did in a Single Line
So to write that all in one line you would just write
people[0].name
Apply to your Question
So to apply to your question if your result looks like this
var result = [
{
"name": "Jason"
"date of birth": "february 23, 2985"
....
}
];
Then you need this to get the name
result[0].name
If it's just this
var result = {
"name": "Jason"
"date of birth": "february 23, 2985"
....
}
Then you just need
result.name
As asked in the comment if you want to get the date of birth property out of the object you need to use bracket notation to get the element out of an object. Bracket notation is one of the two object property accessors the other being dot notation. I covered both at the enter the object section. It can be used at anytime, but is usable in some cases that dot notation does not work.
An example and quote from MDN:
get = object[property_name];
object[property_name] = set;
property_name is a string. The string does not have to be a valid identifier; > it can have any value, e.g. "1foo", "!bar!", or even " " (a space).
So since certain character like spaces can't be used in dot notation bracket notation must be used in those special cases when those characters are present.
Below is the bracket notation of the date of birth.
result["date of birth"]
Like I said before it can be used anywhere, but generally dot notation is preferred for its brevity. So just to show that, we will show the name field being accessed using bracket notation:
result["name"]
One additional reason you may want to use bracket notation is for its ability to use variables like so.
var prop_name = "date of birth";
result[prop_name];
which actually if you understand the principle of that example the MDN example might make more sense.
If you have a question feel free to leave me a comment.

Converting non-JSON text string into JSON object

I have this simple string repeated 1000's of times for worldwide weather widget I'm using in a large text file:
City Name = "Albuquerque, NM, US" Location = "NAM|US|NM|ALBUQUERQUE" Country = "United States"
Notice how it's formatted.
CITY NAME
LOCATION
COUNTRY
The string I'm actually passing into the Widget is the LOCATION string: "NAM|US|NM|ALBUQUERQUE"
So what I'm trying to do is convert the first example of the CITY NAME, LOCATION and COUNTRY into a JSON OBJECT.
Then, once I have that, I wish to allow the user to INPUT their current location or any location for that matter thereby passing the user input into this:
$('#digiclock').jdigiclock({
// Configuration goes here
clockImagesPath: "images/clock/",
weatherImagesPath: "images/weather/",
am_pm: false,
weatherLocationCode: "NAM|US|TN|CHATTANOOGA",
weatherMetric: "F",
weatherUpdate: "5",
proxyType: "php"
});
The weatherLocationCode is actually the LOCATION in the string above. So when the user inputs their CITY/STATE or just CITY, I want to parse the newly formed JSON object from the above text, capture the location, and stick-it up in the function.
This is what the end result should be:
// JSON OBJECT
{data : [
{
"City Name" : "Aachen, DE",
"Location" : "EUR|DE|GM011|AACHEN",
"Country" : "Germany"
}
]
}
Simple? Not so much.