c# - Int not getting displayed as string in JSON Object -




i trying loop through list of json objects , convert them indexed json.

below code have written:

private string itemsasjson(list<string> jsonitemlist) {     string itemasjson = "";     (int = 0; < jsonitemlist.count; i++)     {         string index = i.tostring();         itemasjson += "{ " + index + " : " + jsonitemlist[i] + "},";     }     return itemasjson; } 

but object below here:

{     0: {         "item_type": "batch",         "item_id": "82",         "bill_item_name": "a z",         "quantity": "1",         "inventory_id": "82",         "individual_price": "2.90",         "batch_no": "", 

how convert 0 in above text string ("0")?

the best answer use json parser library, json.net, , not parse json manually.

if reason decide proceed method, can add quotes around index number escaping quote in string \":

private string itemsasjson(list<string> jsonitemlist) {     string itemasjson = "";     (int = 0; < jsonitemlist.count; i++)     {         string index = i.tostring();         itemasjson += "{ \"" + index + "\": " + jsonitemlist[i] + "},";     }     return itemasjson; } 

on side note, if building string this, recommend using stringbuilder instead of +=:

private string itemsasjson(list<string> jsonitemlist) {     stringbuilder builder = new stringbuilder();     (int = 0; < jsonitemlist.count; i++)     {         builder.append("{ \"");         builder.append(i);         builder.append("\":");         builder.append(jsonitemlist[i]);         builder.append("},");     }     return builder.tostring(); } 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -