Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
"dateOfBirth": "1974-06-10",
            "ethnicGroup": "",
            "historicalIDs": [],
            "homeDeployment": null,
            "iDs": [
                {
                    "id": "E6051954",
                    "type": "EPI"
                },
                {
                    "id": "51005383",
                    "type": "IDX"
                },
                {
                    "id": "51005383",
                    "type": "IDXUNPAD"
                },
                {
                    "id": "500102779",
                    "type": "CHS"
                },
                {
                    "id": "18706344",
                    "type": "STAR"
                },
                {
                    "id": "0005700581",
                    "type": "N"
                },
                {
                    "id": "0005700581",
                    "type": "D"
                },
                {
                    "id": "0050750170",
                    "type": "E"
                },
                {
                    "id": "0050501702",
                    "type": "U"
                },
                {
                    "id": "0050501702",
                    "type": "C"
                },
                {
                    "id": "0050501702",
                    "type": "K"
                },
                {
                    "id": "0050501702",
                    "type": "M"
                },
                {
                    "id": "0050501702",
                    "type": "S"
                },
                {
                    "id": "0050501702",
                    "type": "R"
                },
                {
                    "id": "     Z6287",
                    "type": "INTERNAL"
                },
                {
                    "id": "Z6287",
                    "type": "EXTERNAL"
                }
            ],
            "maritalStatus": "Single",
            "name": "Interra,Craig"



this is JSON code and I want to display id of TYPE=CHS. Always it changes the sequence so Can anyone give me the code for display id particular only for CHS.

What I have tried:

dynamic dJson3 = dJson["iDs"];
                   Log.Write("Value in djson3 " + dJson3.ToString());
                   //for(int i= 0; i < 20; i++)
                   //{
                   //    if(dJson3["type"].ToString()=="CHS")
                   //    {
                   //        if (!string.IsNullOrWhiteSpace(dJson3["id"].ToString()))
                   //        {
                   //            kv.Add("Medical Record Number", dJson3["id"].ToString());

                   //        }
                   //    }
                   //}
                   var itemsA = dJson3["iDs"];
                   foreach (var item in itemsA)
                   {
                       if (dJson3["type"].ToString() == "CHS")
                       {
                           if (!string.IsNullOrWhiteSpace(dJson3["id"].ToString()))
                           {
                               kv.Add("Medical Record Number", dJson3["id"].ToString());

                           }
                       }
                   }



I tried this two method but not getting proper answer, getting exception of like
The best overloaded method match for 'System.Collections.Generic.Dictionary<string,object>.this[string]' has some invalid arguments
or cannont convert string to int
Posted
Updated 20-Jun-22 3:33am

Your code is very confused.

You start with an object containing a property called iDs, stored in your dJson variable.

You retrieve the iDs property value, and store it in your dJson3 variable.
C#
dynamic dJson3 = dJson["iDs"];
You then try to retrieve the value of the non-existent iDs property from the value of the iDs property, and store that in your itemsA variable.
C#
var itemsA = dJson3["iDs"]; // There is no iDs.iDs property on your object!
You then loop over the list, checking the type property on the value of the first iDs property rather than the individual list item.
C#
foreach (var item in itemsA)
{
    if (dJson3["type"].ToString() == "CHS") // dJson3 does NOT point to the item in the list!
    {
        kv.Add("Medical Record Number", dJson3["id"].ToString()); // Same problem here!


Change your code to use the correct JSON paths:
C#
dynamic dJson3 = dJson["iDs"];
foreach (var item in dJson3)
{
    if (item["type"] == "CHS")
    {
        kv.Add("Medical Record Number", item["id"].ToString());
    }
}
 
Share this answer
 
Check out the article that I wrote: Working with JSON in C# & VB[^] ... it should have answers for you.
 
Share this answer
 

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