0

I have this structure:

{
   "longUrl" :"http://www.sample.com",
   "bok" :1,
   "url" :"http://pleasegetme.com ",
   "title" :""
}
//equivalent
    "{
       \n   \"longUrl\" :\"http://www.sample.com/\",
       \n   \"bok\" :1,
       \n   \"url\" :\"http://pleasegetme.com \",
       \n   \"title\" :\"\"\n
     }"

I have this function

public string Domain1Helper(string longText)
{
    Regex rgxUrl = new Regex("\"url\":\"(.*?)\"");
    Match mUrl = rgxUrl.Match(longText);

    string url = Regex.Replace(mUrl.Groups[1].Value, @"\\", "");
    return url;
}

What I want to get is http://pleasegetme.com.

What is the wrong in my Domain1Helper method?

5
  • 4
    That is a JSON string. Use a json deserializer. Commented Aug 3, 2016 at 9:20
  • no, just a string. Commented Aug 3, 2016 at 9:20
  • 2
    @AJB - yes it is "just a string", but a string containing data in JSON format. Commented Aug 3, 2016 at 9:21
  • Regex is slightly wrong. You forgot the space in '"url" :' Commented Aug 3, 2016 at 9:26
  • @kumarch1, I rollback your edit. The OP is not aware it's a JSON (so adding comment into question "it's json string" is wrong edit). I also change formatting, while you didn't. Pay attention to "was changed" warning when you are editing next time please. Commented Aug 3, 2016 at 9:29

4 Answers 4

2

What you have there is a JSON string. You can parse this using a library called Json.Net. You can find this as a nuget package. You can then use the following code to pick out the strings you want.

JObject jo = JObject.Parse(longtext);
Console.WriteLine(jo["longUrl"].Value.ToString()); // Outputs 'http://www.sample.com'
Sign up to request clarification or add additional context in comments.

Comments

1

AJB, You have an error in your RegEx. That being said you should use a JSON deserializer, like JSON.NET. In the function Domain1Helper it should be:

Regex rgxUrl = new Regex("\"url\"\\s+:\"(.*?)\"");

Notice the \s+ ?

Comments

0

You have included the escape strings \. If you remove those, it should parse just fine, as in here:

https://regex101.com/r/hK7xR7/1

Note, that you method should look like this:

public string Domain1Helper(string longText)
{
    Regex rgxUrl = new Regex(@"\"url\" :\"(.*?)\"");
    Match mUrl = rgxUrl.Match(longText);

    string url = Regex.Replace(mUrl.Groups[1].Value, @"\\", "");
    return url;
}

And alternative solution would be using one json library, which is outlined here: https://msdn.microsoft.com/en-us/library/cc197957(v=vs.95).aspx

Comments

0

It is a freaking JSON formatted string. Use a JSON deserializer such as JSON.NET. I'll leave it to you on which one you'd want to use.

Here is the root object generated using json2csharp.com

public class RootObject
{
    public string longUrl { get; set; }
    public int bok { get; set; }
    public string url { get; set; }
    public string title { get; set; }
}

Example using JSON.NET:

using Newtonsoft.Json; // Namespace is something like this


string json = "{
                 \n   \"longUrl\" :\"http://www.sample.com/\",
                 \n   \"bok\" :1,
                 \n   \"url\" :\"http://pleasegetme.com \",
                 \n   \"title\" :\"\"\n
               }";

RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json);
string url = rootObject.url;
// Do something with the url

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.