Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am in trouble to separate the required data from the string array

this code is working fine

public void show_data()
   {
        string queryString = "email=abc@abc.com&isVerified=true";
        NameValueCollection nvc = StringToNameValueCollection(responseText);
       
        if (nvc["email"] != "")
        {
           userName = nvc["email"].ToString();
        }
   }
  NameValueCollection StringToNameValueCollection(string queryString)
    {
        NameValueCollection queryParameters = new NameValueCollection();
        string[] querySegments = queryString.Split('&');
        foreach (string segment in querySegments)
        {
            string[] parts = segment.Split('=');
            if (parts.Length > 0)
            {
                string key = parts[0].Trim(new char[] { '?', ' ' });
                string val = parts[1].Trim();

                queryParameters.Add(key, val);
            }
        }

        return queryParameters;
    }


but when i modified the code

public void show_data()
   {
        string queryString = {"id": "1234", "name": "ABC XYZ", "given_name": "ABC", "family_name": "XYZ", "link": "http://profiles.google.com/123456", "gender": "male", "locale": "en-GB"};

        NameValueCollection nvc = StringToNameValueCollection(responseText);
       
        if (nvc["email"] != "")
        {
           userName = nvc["email"].ToString();
        }
   }

 NameValueCollection StringToNameValueCollection(string queryString)
    {
        NameValueCollection queryParameters = new NameValueCollection();
        string[] querySegments = queryString.Split(',');
        foreach (string segment in querySegments)
        {
            string[] parts = segment.Split(':');
            if (parts.Length > 0)
            {
                string key = parts[0].Trim(new char[] { '?', ' ' });
                string val = parts[1].Trim();
                Response.Write(key + ":" + val);
                queryParameters.Add(key, val);
            }
        }
        return queryParameters;
    }


here queryparameters returns the output like this
{"id":"1234" "name":"ABC XYZ" "given_name":"ABC" "family_name":"XYZ" "link":"http "gender":"male" "locale":"en-GB"}

Here i want to also reduce the all the double quotes(")

i want solution to gid rid of the double qoutes(")


thanks,
Deepak

[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 11-Oct-11 22:43pm
v4

Well, you could get rid of them at source:
C#
string queryString = queryString.replace("\"", "");
If you did this at the top of the StringToNameValueCollection method, it would work.
 
Share this answer
 
Hi,

try this once

your querystring value is like following

C#
string queryString = "id:1234,name:ABC XYZ,given_name:ABC,family_name: XYZ, link: http://profiles.google.com/123456,gender:male,locale:en-GB";


All the Best
 
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