0

How can I convert a String like this into an Array List

 {'id':'6726','Codigo':'AlmNumTrab','Denominacion':'Almacen','Descripcion':'Almacenes','TipoCampo':'2','TipoIntroduccion':'0'}

I would like to get the id field directly like datos[0]

Thank you

0

4 Answers 4

1

you didn't mention the language search google as key value array
here the code for c# :

var Dictionary= new Dictionary<string, object>();
dictionary.Add("id", "6726");

object id= dictionary["id"];
Sign up to request clarification or add additional context in comments.

Comments

0
  1. use the Json.Net nuget package
  2. make a class yourClassHere (or copy paste json as class in visual studio) with matching properties.
  3. use this code

Untested but should do the trick:

string yourStringIsJson = "{'id':'6726','Codigo':'AlmNumTrab','Denominacion':'Almacen','Descripcion':'Almacenes','TipoCampo':'2','TipoIntroduccion':'0'}";

yourClassHere deserializedThing = JsonConvert.DeserializeObject<yourClassHere>(yourStringIsJson);

Comments

0

This is a JSON string. so best to treat it as such.

So With C# add the Newtosoft JSON library with NuGet.

then you can deserialise it to a Dictonary

var dict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>("{'id':'6726','Codigo':'AlmNumTrab','Denominacion':'Almacen','Descripcion':'Almacenes','TipoCampo':'2','TipoIntroduccion':'0'}");

you can then access the values like this:

dict["id"] // this should give you 6726

Comments

0

Thank all for your answers.

I finally found a solution in a simple line, is not an ArrayList but I think is better solution.

String idCampo = JObject.Parse(datos)["id"].ToString();

As you know this converts the string in an JObject and I can access it directly.

I will try your answers anyway, just for learning.

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.