Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"}]

What I have tried:

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 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;

}
Posted
Updated 17-May-21 6:37am
Comments
OriginalGriff 17-May-21 11:34am
   
Do you have a question?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

Use the "Improve question" widget to edit your question and provide better information.

1 solution

You didn't tell us what the actual error is:

Is the for loop ever entered?
Or, does it cause an error when the for loop is entered?

I'm going to guess that the for loop is never entered.
That means the JSONObj.length is probably 0.

Most likely the problem is related to how you think the object is deserialized.

You actually missed a closing } at the end of your sample JSON.
Keep in mind that this means the returned thing from the following json object is an object -- not an array.
JavaScript
{"projects":[{"name":"projects/sTest"},{"name":"projects/A345"},{"name":"projects/off"},{"name":"projects/dds"}]}

The outer {} means that there is an object returned.
That object {} contains an array.

That probably means that when you call the following line:
JavaScript
var JSONObj = JsonConvert.DeserializeObject(result);

You get an object back.
What is the Length value of the object?
It isn't defined and probably gives you an error.
What you need to do is inspect JSONObj to see if it has a member named "projects".
That's probably the array of objects you are attempting to iterate over.
But, we need to know more about the result you are seeing to know more.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900