从字符串数组中分离数据的问题 [英] Problem With Seprating Data From string Array
问题描述
你好
我很难从字符串数组中分离出所需的数据
该代码可以正常工作
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;
}
在这里,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"}
在这里,我还想减少所有双引号()
我想要解决方案摆脱双qoutes()
谢谢,
迪帕克
[edit]添加了代码块,将我的内容作为纯文本..."选项已禁用-OriginalGriff [/edit]
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]
推荐答案
嗯,您可以摆脱来源:
Well, you could get rid of them at source:
string queryString = queryString.replace("\"", "");
如果在StringToNameValueCollection方法的顶部执行此操作即可.
If you did this at the top of the StringToNameValueCollection method, it would work.
尝试一次
您的querystring值类似于以下
Hi,
try this once
your querystring value is like following
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
这篇关于从字符串数组中分离数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!