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?

BWA
  • 5,672
  • 7
  • 34
  • 45
ajbee
  • 3,511
  • 3
  • 29
  • 56
  • 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 – Dieter B Jul 27 '16 at 08:11
  • 2
    See http://stackoverflow.com/questions/11260631/convert-json-into-class-object-in-c-sharp for the JSON deserialize method -- Which I recommend over the substring solution – Dieter B Jul 27 '16 at 08:12
  • Yeah, strongly recommend the JSON way, not the string/substring/replace method. – Gil Sand Jul 27 '16 at 08:33

4 Answers4

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

BWA
  • 5,672
  • 7
  • 34
  • 45
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;
Shakir Ahamed
  • 1,290
  • 3
  • 16
  • 39
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;
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • Removal of \ is obsolete because after deserialization it has gone. Your json sample string is wrong (missing the "\"). See http://json.org for specs – Sir Rufo Jul 27 '16 at 08:18
  • 1
    One error left: The json contains an array of objects ;o) – Sir Rufo Jul 27 '16 at 08:21
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, @"\", "");
C4d
  • 3,183
  • 4
  • 29
  • 50