3

I have this string

[
    {
        "url_short":"http:\/\/sample.com\/8jyKHv",
        "url_long":"http:\/\/www.sample.com\/",
        "type":0
    }
]

What I want is to get http:\/\/sample.com\/8jyKHv and translate it to

http://sample.com/8jyKHv

Is it possible?

3
  • You have 2 options here. Either you treat this as a string and use substring and replace. Either you treat this as a JSON and do a deserialize into a class object. Both will yield to the desired result Commented Jul 27, 2016 at 8:11
  • 2
    See stackoverflow.com/questions/11260631/… for the JSON deserialize method -- Which I recommend over the substring solution Commented Jul 27, 2016 at 8:12
  • Yeah, strongly recommend the JSON way, not the string/substring/replace method. Commented Jul 27, 2016 at 8:33

4 Answers 4

4

This string is JSON.

You can parse it by using JSON.NET.

Example:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class RootObject
{
    public string url_short { get; set; }
    public string url_long { get; set; }
    public int type { get; set; }
}

public class Program
{
    static public void Main()
    {
        string j = "[{\"url_short\":\"http:\\/\\/sample.com\\/8jyKHv\",\"url_long\":\"http:\\/\\/www.sample.com\\/\",\"type\":0}]";

        List<RootObject> ro = JsonConvert.DeserializeObject<List<RootObject>>(j);

        Console.WriteLine(ro[0].url_short);                 
    }    
}

Response:

http://sample.com/8jyKHv

Sign up to request clarification or add additional context in comments.

Comments

3

Try this

Create a class like below

Note : you can use Paste Special option in visual studio to generate all the classes related to the JSON

Edit -> Paste Special -> Paste Json As Classes

it will create all the classes related to the JSON

    public class url_details
    {
        public string url_short { get; set; }
        public string url_long { get; set; }
        public int type { get; set; }
    }


    public List<url_details> json_deserialized()
    {
        string json = "[{\"url_short\":\"http:\\/\\/sample.com\\/8jyKHv\",\"url_long\":\"http:\\/\\/www.sample.com\\/\",\"type\":0}]";

        List<url_details> items = new List<url_details>();
        items = JsonConvert.DeserializeObject<List<url_details>>(json);

        return items;
    }

And you can access the element like below

   List<url_details> obj = json_deserialized();

   string url_short = obj[0].url_short;

Comments

2

The JSON way is for sure recommended, but cant tell much about it. Here's the alternative way with regex:

Regex rgxUrl = new Regex("\"url_short\":\"(.*?)\",\"");
Match mUrl = rgxUrl.Match(yourString);

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

2 Comments

must be Regex.Replace(mUrl.Groups[1].Value, @"\\", "");
@AJB nope. @ is already escapting the string. Either "\\" or @"\".
2

The string is a JSON string so you can create a class to get the values like this

public class Rootobject
{
    public Class1[] Property1
    {
        get;
        set;
    }
}

public class Class1
{
    public string url_short
    {
        get;
        set;
    }
    public string url_long
    {
        get;
        set;
    }
    public int type
    {
        get;
        set;
    }
}

After this class you can get the data like this

string json = "[{"url_short":"http:\/\/sample.com\/8jyKHv","url_long":"http:\/\/www.sample.com\/","type":0}]";
List<Rootobject> ro = JsonConvert.DeserializeObject<List<Rootobject>>(json);
string ururl = ro[0].Propert1[0].url_short;

2 Comments

Removal of \ is obsolete because after deserialization it has gone. Your json sample string is wrong (missing the "\"). See json.org for specs
One error left: The json contains an array of objects ;o)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.