0

Doing a loop over this json, I always need to be able to get this value "8047" from "value_name":"https://www.portalinmobiliario.com/venta/departamento/estacion-central-metropolitana/8047-edificio-caiquen-nva"

that you can see in the json,this is kind of big so it is easier if you use ctrl + F and look for it, but I put the complete json so you can have a good reference of the way to the variable I actually need to get.

  "results":[
      {
         "id":283495,
         "user":{
            "id":204841198,
            "nickname":"RICARDOCAMPOSANO",
            "registration_date":"2016-02-01T15:18:01Z",
            "first_name":"Ricardo",
            "last_name":"Camposano",
            "country_id":"CL",
            "email":"ricardo.camposano26@gmail.com",
            "phone":{},
            "alternative_phone":{},
            "identification":{},
            "user_type":"normal",
            "tags":[],
            "points":0,
            "site_id":"MLC",
            "permalink":"http://perfil.mercadolibre.cl/RICARDOCAMPOSANO",
            "secure_email":"rcampos.l4jn3q@mail.mercadolibre.cl"
         },
         "item":{
            "id":"MLC462815278",
            "site_id":"MLC",
            "title":"Edificio Caiquén",
            "subtitle":"",
            "seller_id":330106543,
            "category_id":"MLC157523",
            "domain_id":"MLC-DEVELOPMENT_APARTMENTS_FOR_SALE",
            "price":1885,
            "base_price":1885,
            "original_price":0,
            "currency_id":"CLF",
            "price_conversions":[],
            "initial_quantity":330,
            "available_quantity":330,
            "sold_quantity":0,
            "buying_mode":"classified",
            "listing_type_id":"silver",
            "start_time":"2018-07-12T18:50:09Z",
            "stop_time":"2018-08-13T04:00:00Z",
            "condition":"new",
            "permalink":"http://departamento.mercadolibre.cl/MLC-462815278-edificio-caiquen-_JM",
            "thumbnail":"http://www.mercadolibre.com/jm/img?s=STC\u0026v=I\u0026f=processing_image_es.jpg",
            "secure_thumbnail":"https://www.mercadolibre.com/jm/img?s=STC\u0026v=I\u0026f=processing_image_es.jpg",
            "pictures":[],
            "video_id":"",
            "descriptions":[],
            "seller_address":{},
            "seller_contact":{},
            "location":{},
            "attributes":[
               {
                  "id":"ITEM_CONDITION",
                  "name":"Condición del ítem",
                  "value_id":"2230284",
                  "value_name":"Nuevo"
               },
               {
                  "id":"DOWN_PAYMENT",
                  "name":"Pie",
                  "value_id":"",
                  "value_name":"10 %"
               },
               {
                  "id":"RESERVATION_FEE",
                  "name":"Monto reserva",
                  "value_id":"",
                  "value_name":"0 UF"
               },
               {
                  "id":"CMG_SITE",
                  "name":"Sitio de origen",
                  "value_id":"",
                  "value_name":"POI"
               },
               {
                  "id":"CANONICAL_URL",
                  "name":"Url canónica",
                  "value_id":"",
                  "value_name":"https://www.portalinmobiliario.com/venta/departamento/estacion-central-metropolitana/8047-edificio-caiquen-nva"
               },
               {},
               {},
               {},
               {},
               {},
               {},
               {},
               {},
               {},
               {}
            ],
            "variations":null,
            "listing_source":"portalinmobiliario",
            "status":"active",
            "tags":[

            ],
            "parent_item_id":"",
            "automatic_relist":false,
            "date_created":"2018-07-12T18:50:17Z",
            "last_updated":"2018-07-12T18:50:17Z"
         },
         "variation":{},
         "created_at":"2018-07-12T18:50:22Z",
         "is_guest":false
      }
   ]
}

In javascript I can achieve this by doing:

var id = id_proyecto = valor.item.attributes[4].value_name.match(/\/(\d+)-[^/]+$/)[1];

But I have no idea how to do it in c#. I think is actually more complex.

Any clue how to figure this out?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
victoriana
  • 69
  • 5
  • 1
    Create class with only value_name property then serialize your json into that class object then loop and use regex – Zeeshan Feb 26 '20 at 13:02
  • Hi, you can check out https://stackoverflow.com/questions/11260631/load-json-text-into-class-object-in-c-sharp to see how to convert JSON to an C# object. After this you can check out the documentation for regex here https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex to see how to work with regex in C#. – ZarX Feb 26 '20 at 13:13
  • Did you find a solution to your problems? – mortb Feb 26 '20 at 15:17

1 Answers1

0

Here is a solution not using regex but IndexOf(...) and Substring(...)

var value = valor.item.attributes[4].value_name;
var idxStart = value.LastIndexOf('/');
var idxEnd = value.IndexOf('-', idxStart);
var id = int.Parse(value.Substring(idxStart + 1, idxEnd - idxStart -1));

...and using regex you could do

var value = valor.item.attributes[4].value_name;
var id = int.Parse(Regex.Match(value, @"/(\d+)\-[^/]+$").Groups[1].Value);
mortb
  • 9,361
  • 3
  • 26
  • 44
  • is it necessary to import any library to be able to use Regex in c# console aplication? – victoriana Feb 26 '20 at 15:43
  • just add `using System.Text.RegularExpressions` to the top of your class file. No other references needed – mortb Feb 26 '20 at 15:55
  • tried this: var value = valor.item.attributes[4].value_name; var id= int.Parse(Regex.Match(value, @"/(\d+)\-[^/]+$").Groups[1].Value); and got this error 'System.Text.RegularExpressions.Regex.Match(string, string)' has some not valid arguments – victoriana Feb 26 '20 at 15:57
  • 1
    just changed data type of "value" variable to string and it worked, thanks for your help guys – victoriana Feb 26 '20 at 16:22