Enhance functionality by incorporating JSON data in C# ASP.NET logic

I have received a JSON data sample that looks like the following. I am looking to perform a certain action based on specific conditions within the response:

If success is equal to true and if the length of ParentList is greater than 0, then execute a logic in C#.

{
    "success": "true",
    "Building_rjid": "NVMBBD0015609",
    "ParentList": [
        {
            "Site_sap_id": "I-MU-MUMB-ENB-I134",
            "Site_GeoCoordinates": "19.01612,73.04158",
            "Site_priority": "P1",
            "BearingAngleFromBuilding": 278
        },
        {
            "Site_sap_id": "I-MU-MUMB-ENB-7172",
            "Site_GeoCoordinates": "19.0138,73.041",
            "Site_priority": "P2",
            "BearingAngleFromBuilding": 205
        },
        {
            "Site_sap_id": "I-MU-MUMB-ENB-7202",
            "Site_GeoCoordinates": "19.01841417,73.043785",
            "Site_priority": "P3",
            "BearingAngleFromBuilding": 33
        }
    ]
}

UPDATE

This is how I retrieve the JSON response:

try
        {
            strInputJSON_BldgRJID = (new JavaScriptSerializer()).Serialize(inputBldgRJID);

            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";
            client.Encoding = Encoding.UTF8;
            jsonData = client.UploadString(apiUrl_CheckBldg, strInputJSON_BldgRJID); // json response
            
        }

Answer №1

It seems like you are looking to achieve something similar to the following:

public class ModelX
{
    [JsonPropertyName("success")]
    public bool Success { get; set; }

    [JsonPropertyName("Building_rjid")]
    public bool BuildingRjId { get; set; }

    [JsonPropertyName("ParentList")]
    public List<ParentList> ParentList { get; set; }

}
public class ParentList
{
    // TODO
}
public class Test
{
    static void Main(string[] args)
    {
        var jsonString = "{\r\n    \"success\": \"true\",\r\n    \"Building_rjid\": \"NVMBBD0015609\",\r\n    \"ParentList\": [\r\n        {\r\n            \"Site_sap_id\": \"I-MU-MUMB-ENB-I134\",\r\n            \"Site_GeoCoordinates\": \"19.01612,73.04158\",\r\n            \"Site_priority\": \"P1\",\r\n            \"BearingAngleFromBuilding\": 278\r\n        },\r\n        {\r\n            \"Site_sap_id\": \"I-MU-MUMB-ENB-7172\",\r\n            \"Site_GeoCoordinates\": \"19.0138,73.041\",\r\n            \"Site_priority\": \"P2\",\r\n            \"BearingAngleFromBuilding\": 205\r\n        },\r\n        {\r\n            \"Site_sap_id\": \"I-MU-MUMB-ENB-7202\",\r\n            \"Site_GeoCoordinates\": \"19.01841417,73.043785\",\r\n            \"Site_priority\": \"P3\",\r\n            \"BearingAngleFromBuilding\": 33\r\n        }\r\n    ]\r\n}";
        
        var model = JsonSerializer.Deserialize<ModelX>(jsonString);

        if (model != null && model.Success && model.ParentList.Count > 0)
        {
            // TODO
        }
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Swapping out the document.createElement() method for React.createElement()

In a JavaScript library, I utilize document.createElement() to create an HTMLElement. Now, I am looking to develop a React library with the same functionality. My initial thought was to substitute document.createElement() with React.createElement(). There ...

Invalid JSON format received by Python API

I recently started learning Python and have been working with JSON data. However, I am facing issues with the format of the JSON data that I retrieve using the requests module from an API. webpage = 'https://parser-api.com/parser/arbitr_api/run.php&ap ...

Issues with Content-Security-Policy Implementation in Next JS and AMP Technology

While developing an AMP application with Next JS, everything seems to work perfectly in localhost. However, upon moving to the production environment, I encountered errors related to AMP not being allowed to load its workers. The initial error message is: ...

In React, I'm unable to navigate to a different page

My English may not be the best, but I am working on creating a Login Page. The issue I'm facing is that when I click the Login button, I want to navigate to the Home Page I designed using React. However, whenever I try to implement Link or Route comma ...

Dynamically highlight a MySQL table based on user preferences and store this information permanently

I'm working on a project that involves creating a table with highlight options on each row. The table is generated dynamically from data retrieved from MySQL. Currently, when a user selects a color from a drop-down menu, the color of the row changes a ...

Unable to transform SVG files in Next.js Jest version 28

Currently, my setup involves Next.js version 12.0.7, Jest version 28.1.0, and the use of the next-react-svg library for importing SVG files into components. The configuration in my jest.config.js file looks like this: // jest.config.js const nextJest = re ...

Top approach for a seasoned developer in .NET/C# and C++ to master Java

As a seasoned developer with over 7 years of experience in .NET/C# and 5 years in C++, I am looking for the quickest and easiest way to learn Java. My goal is to master Java programming, utilizing base classes and popular open source libraries while adopti ...

Retrieve the image URL from a JSON object on the domain wikipedia.org

I can't seem to figure out how to retrieve the original value from the json object. I keep getting lost... { "batchcomplete":"", "query":{ "normalized":[{ "from":"dog","to":"Dog" }] , "pages":{ ...

The presence of too many select options can result in a delay in text input responsiveness on an iPad

My HTML form is styled with CSS and functions properly. However, I encountered a delay issue when testing it on an iPad (both iOS7 and 8) - the native keyboard responds very slowly (around 1-2 seconds) to key presses when entering text into the form fields ...

Understanding JSON deserialization in Jackson: TypeReference vs TypeFactory.constructCollectionType

If you want to convert a JSON string into a list of a specific class, there are various methods available. You can refer to this StackOverflow thread for more information. Method 1: Check out the details in this documentation link: List<SomeClass> s ...

Using D3 to access a nested select and sibling from the JSON property of an object

I am attempting to include a sibling before a series of div elements that have been generated using the .enter() method. Here is my current setup: const data = [ { "name": "foo", "hierarchies": [ { "name": " ...

jquery line break in textarea

There is a 'edit description' button that, when clicked, makes text disappear and reappear if (id == 'commedit') jQuery(this).html('<textarea>'+jQuery(this).text()+'</textarea>'); else i ...

How can I show a dynamic popover with specific content when a "Copy" action is triggered?

I am attempting to copy a value to the clipboard: /** * If the browser does not support direct clipboard manipulation, use an alternate method * This is used in the copyTextToClipboard function. * * Function found at: https://stackoverflow.com/a/3 ...

Encountering a Dispose exception for the DbContext in a .NET Core web API

Hi there, I am just starting out with c# and .net core. Currently working on a project involving .NET Core Web API 3.1 and EntityFramework. In this project, I have implemented a repository package which follows the Generic Repository Pattern. public interf ...

Does the color of the item change as it gets older?

How can I smoothly transition a color as it ages using a dynamic time scale? I want to calculate the age based on the difference in time rather than triggering an animation after an event. I aim for a seamless gradient transition between two colors over a ...

Navigating through an array object

I am currently utilizing react-select and aiming to iterate through an object in order to display it as the select element's value and label: // Within the render method of the component: var myVar = [ this.props.foo.forEach(function(a){ {valu ...

Challenges associated with utilizing img src in JavaScript

I am facing a simple issue. I have carInfo data retrieved from a JSON file, but I am struggling to correctly parse the img source stored in the variable $imgsrc instead of treating it as a string called "$imgsrc". This data needs to be appended to my HTML ...

Launch a YouTube video within a sleek and stylish Bootstrap modal popup

I am currently extracting video data from my SQL table. The fields in the table are as follows: - sidebar_video_id (auto increment) - sidebar_video_nev - sidebar_video_link (full URL) - sidebar_video_v_id (video ID at the end of the URL) What I'm tr ...

Updating a list of books from a form in another component: Step-by-step guide

Hello everyone, I am fairly new to using React and am currently following a tutorial on creating a book library. In this project, I have an AddNewBook Component which is essentially a form structured as follows: function AddNewBook(){ const [name, setNam ...

Working with JSON objects in JavaScript using CodeIgniter framework

I am encountering an issue with my Jquery function in the controller. It seems to be unable to read the Json_encoded object, even though I am using codeigniter. Any ideas why? $(this).ajaxSubmit({ type : "POST", u ...