Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Have a paragraph on a website that I am attaching to a string variable.

Looks something like this:

{
"id": "499156235",
"login_url": "https://www.testing.com/platform/test_account_login.php?user_id=499156235",
"email": "mhuvdfmt_greenesteerin@tfbnw.net",
"password": "1752836968"
}


I want to create two variables that contain the values from Email and Password.
So using the above example i would have a variable with mhuvdfmt_greenesteerin@tfbnw.net

and a variable with 1752836968

Tried using Regex and split but can't seem to get it to work.
Posted
Comments
Sergey Alexandrovich Kryukov 25-May-11 15:41pm    
Not clear what seems to be a problem. The variables you mentioned are already available in your sample. Please show what you tried and elaborate.
--SA
Member 7954531 25-May-11 16:53pm    
So i did a split on ',' ':' and '"'.
And have that in an array.
I want to be able to set a variable so password = 1752836968 and email = mhuvdfmt_greenesteerin@tfbnw.net.

Code i'm using so far:

string Info = driver.FindElement(By.XPath("//body/pre")).Text;
char[] sep = {',', ':', '"'};
string[] split = Info.Split(sep);
foreach (var s in split )
{
Console.WriteLine(s);
}

info is the variable with the paragraph i pull from a webpage where i gave an example what it looks like above.
Sergey Alexandrovich Kryukov 25-May-11 20:48pm    
OK, now we need a sample if info. How this is related to the structure shown above in the question? Where is the definition of this structure?
--SA
Member 7954531 25-May-11 20:57pm    
I go to a website. on that website is text like:

{
"id": "499156235",
"login_url": "https://www.testing.com/platform/test_account_login.php?user_id=499156235",
"email": "mhuvdfmt_greenesteerin@tfbnw.net",
"password": "1752836968"
}

the variable Info is that text. I'm trying to take a string out of the string info. The string i am trying to take out is the password and email.

Try,
C#
string pattern1 = "(?<=\"email\":\x20\").*?(?=\")";
string pattern2 = "(?<=\"password\":\x20\").*?(?=\")";
string email = System.Text.RegularExpressions.Regex.Match(input, pattern1).Value;
string password = System.Text.RegularExpressions.Regex.Match(input, pattern2).Value;
 
Share this answer
 
v3
Comments
Member 7954531 25-May-11 17:01pm    
Almost!
The password works great but the email coming out as:
email": "mhuvdfmt_greenesteerin@tfbnw.net", "password": "1752836968
yesotaso 25-May-11 17:06pm    
I dont exactly know what input is I just modified a pattern in my closet:)
Edit: There might be a white-space hideously sneaking there messing up patterns :)
Member 7954531 25-May-11 17:15pm    
Doing that leaves email blank.
console.writeline("Email:" + email);

is producing Email:
with nothing.

Password is working though.

right now my code is:

string Info = driver.FindElement(By.XPath("//body/pre")).Text;
string pattern1 = "(?<=\"email\":\x20\").*(?=\"),";
string pattern2 = "(?<=\"password\":\x20\").*(?=\")";
string email = System.Text.RegularExpressions.Regex.Match(Info, pattern1).Value;
string password = System.Text.RegularExpressions.Regex.Match(Info, pattern2).Value;
Console.WriteLine("email:" + email);
Console.WriteLine("Password:" + password);
yesotaso 25-May-11 17:18pm    
Sorry about that the comma shouldnt be there.
And I used this as reference and it seems to work:
string input = "{" + Environment.NewLine + "\"id\": \"499156235\"," + Environment.NewLine +
"\"login_url\": \"https://www.testing.com/platform/test_account_login.php?user_id=499156235\"," + Environment.NewLine +
"\"email\": \"mhuvdfmt_greenesteerin@tfbnw.net\"," + Environment.NewLine +
"\"password\": \"1752836968\"" + Environment.NewLine + "}";
Member 7954531 25-May-11 17:23pm    
think i typed that wrong. it's coming out as:
mhuvdfmt_greenesteerin@tfbnw.net", "password": "1752836968

It doesn't stop and continues on to the rest
Boy oh boy. No one has noticed that this is JSON yet.

I continue to use the solution from the: StackOverflow Question[^].

Don't use the approved answer, but instead use the 16 Up-voted answer with the code in the page. He fixed a bug with the linked library in the "answer".
 
Share this answer
 
Comments
yesotaso 25-May-11 17:04pm    
well I saw it before, but didnt know thats good old jason :P
http://www.imdb.com/title/tt0211443/
Sergey Alexandrovich Kryukov 25-May-11 20:50pm    
Good catch, but I hate even the idea of guessing... Let's see...
--SA
Kim Togo 26-May-11 6:52am    
My 5. I think the best solution is to use a JSON class that can handle the format. Just like XElement can.
Split over "," then split last two items in the array over ":" to get the username and password. Share the code you have tried so far so that someone can provide better help.
 
Share this answer
 
Greetings... it would seem you are just attempting to rip your string apart... many different ways to do it, but only recommended if you can count on the string being formatted a particular way every time... if so, try something like... (assuming testString is has the content you displayed)

C#
string[] items = testString.Split(',');

Dictionary<string, string> info = new Dictionary<string, string>();
foreach (string item in items)
{
    string[] subItems = item.Split(":".ToCharArray(), 2);
    info.Add(subItems[0], subItems[1]);
}


Good luck!
 
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