locked
How to get value not text from the jason result result RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    I got @Html.ListBox items and their values by using jquery. The format for data like below

    40150:Resource,40153:Country Admin Assign,40161:Cross Country Request,40151:Email Logs,40154:AV Programme,40158:Country Module Management Report,40160:User Report,40149:Role Settings

    I want only value part in c#. Like from  40150:Resource, I just need 40150.

    And I need to access all values.

    Friday, June 7, 2019 7:26 AM

Answers

  • User61956409 posted

    Hi demoninside9,

    I want only value part in c#. Like from  40150:Resource, I just need 40150.

    You can refer to the following code to extract data from your example string.

    var selectedval = "40150:Resource";
    var code = selectedval.Split(':')[0];

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 7, 2019 9:11 AM

All replies

  • User61956409 posted

    Hi demoninside9,

    I want only value part in c#. Like from  40150:Resource, I just need 40150.

    You can refer to the following code to extract data from your example string.

    var selectedval = "40150:Resource";
    var code = selectedval.Split(':')[0];

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 7, 2019 9:11 AM
  • User-2054057000 posted

    First you create your class based on the json structure which you have. You can do this by using this online tool called - http://json2csharp.com/.

    Next in your project, after you have read the json, you can Deserialize it by using JsonConver.cs class. Check the below code:

    string jsonResponse = "";
    using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
    {
    StreamReader reader = new StreamReader(response.GetResponseStream());
    jsonResponse = reader.ReadToEnd();
    }
    /*End*/

    /*http://json2csharp.com*/
    MyJsonClass rootObject = JsonConvert.DeserializeObject<MyJsonClass>(jsonResponse);

    Now all the values of the json can be accessed using the class object called rootObject.

    You can also refer this tutorial which does this same thing.

    Sunday, June 9, 2019 3:10 AM