Adhikar Patil

Adhikar Patil

  • NA
  • 481
  • 127.3k

How to filter Json Response Dynamically in C#

May 18 2020 1:02 PM
Hello,
I am having json response like as below
 
{
"projects": [
{
"country_name": "Afghanistan",
"activity_name": "100 MW of Additional Generating Capacity for Kabul",
"sector_name": "Economic Growth and Trade",
"start_date": "2007-05-24T00:00:00.000",
"end_date": "2010-05-31T00:00:00.000",
"description": "In 2003, USAID committed to providing emergency diesel-generated power to the key southern cities of Kandahar (Kandahar Province), Lashkar Gah (Hilmand Province), and Qalat (Zabul Province). The World Bank provides emergency diesel generators to eight provincial cities, including Aybak (Samangan Province), Musa Qala (Hilmand Province), and Tirin Kot (Uruzgan Province). Providing diesel power to these areas was a short-term way to provide immediate sources of electrical while longer-term and more sustainable lower-cost solutions were being developed. The delay in longer-term solutions and the changing reality over the past four years makes it clear that the temporary diesel power stations must be converted to permanent installations. In addition, the stations have been in use for much longer than initially planned, requiring continued operations and maintenance support. USAID is continuing to provide technical support and is developing the capacity of Da Afghanistan Breshna Sherkat (Afghanistan National Electricity Corporation ? DABS) to manage these power plants.",
"partner_name": "Louis Berger Group, Inc."
},
{
"country_name": "Afghanistan",
"activity_name": "Accelerating Sustainable Agriculture Program (ASAP)",
"sector_name": "Agriculture and Food Security",
"start_date": "2006-11-22T00:00:00.000",
"end_date": "2011-10-31T00:00:00.000",
"description": "This project is designed to revitalize and improve global competitiveness of Afghanistan's agriculture sector. The objective is to help Afghanistan develop a dynamic agriculture system capable of adapting to market forces by engaging activities that will lead to: a) increased competitiveness of Afghan agriculture products in domestic and international markets for high-value commodities; and b) improved public policies and institutions in support of more competitive agriculture and agribusiness. To achieve competitiveness and a sound policy environment, ASAP supports market linkages, improves farm technology and practices, improves post-harvest activities to meet market requirements, develops and enhances business organizations to provide support services to small-and medium-scale agribusinesses, and assists in production and post-harvest infrastructure construction. Since June 2011, the program has been transitioning the majority of its activities and facilities to the Ministry of Agriculture, Irrigation, and Livestock. ASAP closes at the end of September 2011.",
"partner_name": "CI-Chemonics International"
},
{
"country_name": "Afghanistan",
"activity_name": "Advancing Afghan Agriculture Alliance",
"sector_name": "Agriculture and Food Security",
"start_date": "2006-09-14T00:00:00.000",
"end_date": "2011-03-31T00:00:00.000",
"description": "This project, implemented through a Global Development Alliance with Purdue University, provides technical assistance to Kabul University and the Ministry of Agriculture, Irrigation and Livestock (MAIL). The technical assistance provided will support curriculum reform at Kabul University, establishing or strengthening linkages between the university and related ministries, strengthening the skills of professors, and strengthening agriculture extension and research activities of the Ministry. Major components include: a) short-term training for university staff; b) long-term training for masters and undergraduate students at regional universities; c) job placement and internship activities; and d) development of new course curricula and support materials. The project will increase the capacity of participating Afghan universities to provide quality education to their students. Additionally, the project will raise the level of expertise of graduating students to provide Afghanistan with an educated workforce required for successful development of the agriculture sector",
"partner_name": "Purdue University"
},
{
"country_name": "India",
"activity_name": "Advisor to the Secretariat of the Inter-Ministerial Commission for Energy (ICE)",
"sector_name": "Democracy, Human Rights and Governance",
"start_date": "2007-09-01T00:00:00.000",
"end_date": "2011-12-31T00:00:00.000",
"description": "Afghanistan's public electricity sector lacks adequate levels of human resources and organization. The Government of the Islamic Republic of Afghanistan and the international community have invested hundreds of millions of dollars to develop and build a wide variety of projects to improve electricity supply. One major project for increasing the power supply to Kabul is the North-East Power System, supported by many stakeholders and donors. To bring together the various ministries that have an impact on the electric power sector, the government created the Inter-Ministerial Commission for Energy (ICE) and appointed a secretariat to support it. The government also created a working group to support the Ministry of Energy and Water. The objective of this USAID-funded project is to provide long-term technical assistance and support through advisors to the ICE Secretariat. Advising the ICE involves a multi-disciplined, multi-pronged approach within the Afghan government and the donor community to produce positive and sustainable results. The Asian Development Bank, USAID, the World Bank, GIZ (a federally owned organization supporting the German Government in achieving its development objectives), and other donors provide technical advisors. Together, these advisors provide inputs to the ICE Secretariat to develop solutions in their areas of responsibilities and expertise."
},
{
"country_name": "India",
"activity_name": "Afghan Civil Service Support Program (ACSS)",
"sector_name": "Democracy, Human Rights and Governance",
"start_date": "2007-02-01T00:00:00.000",
"end_date": "2012-03-20T00:00:00.000",
"description": "This project supports civil service reform and training. The project raises the skill level of Afghan civil servants; modernizes, institutionalizes, and harmonizes administrative systems across the ministries; and supports a system for civil service training within the Afghan government.",
"partner_name": "Deloitte Consulting, LLP."
},
{
"country_name": "Afghanistan",
"activity_name": "Afghan Clean Energy Program (ACEP)",
"sector_name": "Science, Technology and Innovation",
"start_date": "2009-09-08T00:00:00.000",
"end_date": "2012-01-31T00:00:00.000",
"description": "USAID is committed to building the capacity of the Afghan government to provide its people with clean, renewable energy to power their homes, schools, and businesses. The Afghan Clean Energy Project (ACEP) aims to foster energy independence and development through increased use of renewable energy, increased energy efficiency, and establishment of a local renewable energy equipment and service industry. More than 300 communities will benefit from advanced technologies to power their homes, schools, and businesses through clean, renewable energy, such as micro-hydropower, solar, and wind projects. Finally, the project will provide technical assistance to the Ministry of Energy and Water in key areas of power sector reform",
"partner_name": "International Resources Group, Ltd."
},
{
"country_name": "Afghanistan",
"activity_name": "Afghanistan Parliamentary Assistance Program (APAP II)",
"sector_name": "Democracy, Human Rights and Governance",
"start_date": "2011-11-29T00:00:00.000",
"end_date": "2012-06-28T00:00:00.000",
"description": "This project advances the Afghan Parliament's institutional, technical, and political development. The project works with members in the upper and lower houses, parliament staff, leadership offices, and committees. In addition to legislative support, APAP activities improve Parliament's constituency outreach efforts, communications, information technology, and national budget review."
},
{
"country_name": "Senegal",
"activity_name": "HYVALL",
"sector_name": "Education",
"start_date": "2012-09-30T00:00:00.000",
"end_date": "2014-12-31T00:00:00.000",
"description": "HYVALL will encourage a culture of reading in communities, and strengthening community based organizations to support reading."
},
{
"country_name": "India",
"activity_name": "SHOPS: Strengthening Health Outcomes through the Private Sector",
"sector_name": "Global Health",
"start_date": "2010-09-30T00:00:00.000",
"end_date": "2014-09-30T00:00:00.000",
"description": "SHOPS will be active in FY2012 only because it is a 4-month bridge activity for the MBPH follow-on. Therefore, the scope and geographic focus remains same as MBP project",
"partner_name": "Abt Associates Inc.",
"obligated_amount": "1500000"
},
{
"country_name": "Afghanistan",
"activity_name": "Afghan Energy Capacity Building",
"sector_name": "Economic Growth and Trade",
"start_date": "2008-07-03T00:00:00.000",
"end_date": "2012-01-02T00:00:00.000",
"description": "The goal of the Afghan Energy Capacity Building (AECB) project is to provide technical, managerial, and administrative capacity building in the Afghan energy sector. The Ministry of Energy and Water (MEW), Da Afghanistan Breshna Sherkat, the Ministry of Mines, and other entities will benefit from the project. The project will also support MEW's donor coordination activities and develop a sustainable training organization. The overall approach of the project is to develop meaningful capacity building programs customized to meet the needs of participants while addressing the immediate and longer-term needs of the energy sector.",
"partner_name": "Advanced Engineering Associates International, Inc."
}]
But i want to filter this json dynamically based on key and values in Dot net core.
 I have used following code to pares the response
 
string responseString = await GetAppendUrlResponseString(httpResponseMessage);
var json = JArray.Parse(responseString);
JArray array = new JArray();
string url = baseUrlRequestModel.AppendUrl;
var keyValues = url.Split('?');
var parts1 = keyValues[1].Split('&');
 
foreach (var item in json)
{
for (int i = 0; i < parts1.Length; i++)
{
var SS = parts1[i].Split('=');
if ((string)item[SS[0]].Value<string>() == SS[1])
{
responseString = Convert.ToString(item);
}
else
{
responseString = "";
}
}
if(responseString!="")
array.Add(responseString);
}
But when response filtering it will throws error due to Projects objcts without objects it will run finebut with objects it will through exception.
 
Note: That objects get dynamically Now are Projects, Sometimes it may be list, it may be Users so dont know it is randomly reading json 
 
 

Answers (2)