2

How to get certain values from json string, and insert it into a dictionary in C# ?

The following is my json string:

[{"RowNum":7,"Item_Code":"m2","Item_Name":"Mixer","Item_ProductBrand":0,"Item_Category":1,"Item_Unit":0,"Item_Blocked":false,"Costing_Method":0,"Standard_Cost":0.0,"Retail_UnitPrice":10.0,"Dealer_UnitPrice":20.0,"Distributor_UnitPrice":30.0,"Safety_Stock":0.0,"Safety_LeadTime":0,"Reorder_Point":0.0,"Reorder_Quantity":0.0,"BusinessUnitID":"1","CompanyID":"1","RowVersion":1},{"RowNum":8,"Item_Code":"t1","Item_Name":"Television","Item_ProductBrand":0,"Item_Category":1,"Item_Unit":0,"Item_Blocked":false,"Costing_Method":0,"Standard_Cost":0.0,"Retail_UnitPrice":40.0,"Dealer_UnitPrice":50.0,"Distributor_UnitPrice":60.0,"Safety_Stock":0.0,"Safety_LeadTime":0,"Reorder_Point":0.0,"Reorder_Quantity":0.0,"BusinessUnitID":"1","CompanyID":"1","RowVersion":1}]

Currently I have two json objects in this string. It is actually a string created in the form of JSON Array.

I have a Dictionary, into which I need to add Item_Name and Item_Code. How to do that?

I was successful in creating the Dictionary and adding/mapping elements from the string. But, I cannot populate the DropDownList. My code is :

string items = GetJSON(url);
Dictionary<string, string>[] itemDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(items);
DropDownList3.DataSource = itemDictionary;
DropDownList3.DataTextField = "Item_Name";
DropDownList3.DataValueField = "Item_Code";
DropDownList3.DataBind();

This is not working. How to correct this ?

1

1 Answer 1

2

you should create a list, you shouldn't bind the dictionary to combobox, I don't think you even can. May be I am wrong but it is not the clean way. So what you can do is to create a list of objects, and from the dictionary create an object, for example Item object similar to what I wrote below, then add the object to the list.And finally set the list as the data source. That will work. something like this:

public class item
{
    public Customer(){
    }

    public int ItemCode{set; get;}
    public string ItemName{set; get;} 

   // and the other properties 

} 

then from the dictonary create the object and add it to the list.

List<Item> itemList = new List<Item>();
string items = GetJSON(url);
Dictionary<string, string>[] itemDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(items);
Item newItem = new Item();
newmItem.ItemCode = Convert.ToInt32 (itemDictionary["Item_Code"]); 
// ... and the other properties 
// then add the object to the list 
itemList.Add(newItem);
DropDownList3.DataSource = itemList;
DropDownList3.DataTextField = "ItemName";

DropDownList3.DataBind();

Then bind it to index_changed event and from there cast it to Item object and get the property that you are interested

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.