none
How do i split ReadAsStringAsync value to list array using linq C# RRS feed

  • Question

  • Hi,

    i am using Content.ReadAsStringAsync().Result for receiving output from service.

    i am getting the below format output.

    Code : 

    var Values = response.Content.ReadAsStringAsync().Result;

    Return Output :

    ["AK-01AF,1","MLM-01032,0"]

    Finally i want to split to array using below code. but, it it not working

    var Values = response.Content.ReadAsStringAsync().Result;
    string[] FinalOutput = Valuess.Split(',').ToArray();

    My Final Output should be :

    ["AK-01AF"]    ==> [0-th index]

    ["MLM-01032"] ==> [1st index]

    How to format my output like above...?
    • Edited by Gani tpt Thursday, November 29, 2018 11:24 AM
    Thursday, November 29, 2018 11:05 AM

Answers

  • Check an example:

    string values = "[\"AK-01AF,1\",\"MLM-01032,0\"]";
    
    var s = new DataContractJsonSerializer( typeof( string[] ) );
    string[] elements = (string[])s.ReadObject( new MemoryStream( Encoding.UTF8.GetBytes( values ) ) );
    
    string first = elements[0];
    string second = elements[1];
    

    Also add a reference to System.Runtime.Serialisation.

    • Marked as answer by Gani tpt Thursday, November 29, 2018 2:03 PM
    Thursday, November 29, 2018 1:44 PM

All replies

  • It is very trivial. You can just split each item of array by the , delimiter and then make it an array, then finally use String.Format to print it the way you need like :

    var result = Valuess.Select(x=> x.Split(',').First()).ToArray();
    
    for(int i=0; i < result.Length; i++)
    {
         Console.WriteLine(String.Format("{0} ==> {1}th index)",result[i],i));
    }

    See this demo fiddle which might be helpful.



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites




    • Edited by Ehsan Sajjad Thursday, November 29, 2018 11:59 AM
    Thursday, November 29, 2018 11:53 AM
  • Thanks..

    But, i am getting values in [ ] format. not in { } (braces).

    ["AK-01AF,1","MLM-01032,0"] ==> JSON Format

    So the final output (All values and characters should eliminate)

    Output would be below

    AK-01AF

    MLM-01032


    • Edited by Gani tpt Thursday, November 29, 2018 12:43 PM
    Thursday, November 29, 2018 12:22 PM
  • Check an example:

    string values = "[\"AK-01AF,1\",\"MLM-01032,0\"]";
    
    var s = new DataContractJsonSerializer( typeof( string[] ) );
    string[] elements = (string[])s.ReadObject( new MemoryStream( Encoding.UTF8.GetBytes( values ) ) );
    
    string first = elements[0];
    string second = elements[1];
    

    Also add a reference to System.Runtime.Serialisation.

    • Marked as answer by Gani tpt Thursday, November 29, 2018 2:03 PM
    Thursday, November 29, 2018 1:44 PM
  • Excellent..this is my solution..
    Thursday, November 29, 2018 2:03 PM