TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sujeet Raman
859
927
357.5k
How to check a specific string contains in the Json object in core api
May 17 2021 2:01 PM
Hi
I have a function which will check given project name is include in the json object. For that I am not getting a proper json object in my response how to do that?
this is my object
{"projects":[{"name":"projects/sTest"},{"name":"projects/A345"},{"name":"projects/off"},{"name":"projects/dds"}]
i have one main method which will call public bool CheckDuplicate(string name) method to return whether any same name inside the object or not.
public
async Task<
string
> UploadData(
string
name,
string
url)
{
var hasMatch = CheckDuplicate(name);
//post project_name methode here which will happen only hasmatch is false.need to block same names
}
public
bool
CheckDuplicate(
string
name)
{
string
pname = name.Substring(9, name.Length - 9);
HttpClient client =
new
HttpClient();
client.BaseAddress =
new
Uri(httpRequest);
client.DefaultRequestHeaders.Accept.Add(
new
MediaTypeWithQualityHeaderValue(
"application/json"
));
HttpResponseMessage response = client.GetAsync(httpRequest).Result;
var result = response.Content.ReadAsStringAsync().Result;
in
result i am getting my objects like
{
"projects"
:[{
"name"
:
"projects/sTest"
},{
"name"
:
"projects/A345"
},{
"name"
:
"projects/off"
},{
"name"
:
"projects/dds"
}]
var JSONObj = JsonConvert.DeserializeObject(result);
// i am getting values in JSONObj but below code is not working
//cant check the given pname is there or not
var hasMatch =
false
;
for
(var index = 0; index < JSONObj.length; ++index)
{
var Projects = JSONObj[index];
if
(Projects.name == pname)
{
hasMatch =
true
;
}
else
{
hasMatch =
false
;
}
}
return
hasMatch;
}
Reply
Answers (
10
)
For List converting Which Json Convert Used for asp.net core 3.1
How to pass the list<> in cmd.Parameters.AddWithValue