0

I have a code that access a Api link and retrieves that Json values and stores them in a object list. However when there is nothing to retrieve (Link to the Api doesnt exist) it returns nothing.

However even though the Api link is wrong it still returns Json values. These are their default values that will always be there even though there is nothing that matches the ID requested.

Correct Api Json value

Below link will show values that will always return no matter what

INCorrect Api Json value

here is where i call to the Api

var spidyApi_idByName_result = api_Handler.objFromApi_nameToId(spidyApi_searchIdByName);

Here the Api is accessed and the Json is deserialized.

public RootObject objFromApi_nameToId(string url){
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try{
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream()){
                    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    var jsonReader = new JsonTextReader(reader);
                    var serializer = new JsonSerializer();
                    return serializer.Deserialize<RootObject>(jsonReader);
                }
            }
            catch (WebException){
                throw;
                // used throw as otherwise i can not run code, function requires every path to return
                // Catch never gets called

            }
        }

RootObject class

public RootObject GetApi(string url)
{
    // ...
    return serializer.Deserialize<RootObject>(jsonReader);
}

How do i go around and ignore their "default" json values? just do nothing.

6
  • Does your RootObject have a property called count in it? Commented Apr 3, 2015 at 21:12
  • yes it does. public int count { get; set; } Commented Apr 3, 2015 at 21:15
  • What is the expected system behavior for an incorrect API? Are you expecting it to return a null object from your objFromApi_nameToId method? Commented Apr 3, 2015 at 21:16
  • What do you mean, expected system behavior? Commented Apr 3, 2015 at 21:19
  • When you say ignore their default json values, what do you mean. The json will return. It will deserialize into your root object. But you want to ignore this object because it returned from an invalid api call? Commented Apr 3, 2015 at 21:20

1 Answer 1

1

You need to manage the defaults in your code. E.g. Your invalid API example will always return the count as 0. You can now use that condition to decide your program flow.

RootObject rootObject = serializer.Deserialize<RootObject>(jsonReader);

if (rootObject.count > 0)
    return rootObject;
else
    return null; //or any other program flow you need to execute.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.