Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Value from JSON using JArray

I have the following string (json format)
I have gotten from my server:

{[{"ruta": "1","division": "7"},{"ruta": "2","division": "7"},{"ruta": "3","division":"7"},{"ruta": "4","division": "7"},{"ruta": "5","division": "7"},{"ruta": "23","division": "7"}]}

I want to get each value and save them in string variables in order to save them in a data base.

For that I am trying to do as follows:

JArray jarr = JArray.Parse(result);
foreach (JObject content in jarr.Children<JObject>())
{
    foreach (JProperty prop in content.Properties())
    {
         string tempValue = prop.Value.ToString; // This is not allowed 
         //here more code in order to save in a database
    }
}

But I can't find the way to convert the values to string.

like image 723
adrian4aes Avatar asked Apr 15 '14 22:04

adrian4aes


1 Answers

JArray jarr = JArray.Parse(result);
foreach (JObject content in jarr.Children<JObject>())
{
    foreach (JProperty prop in content.Properties())
    {
         string tempValue = prop.Value.ToString(); // This is not allowed 
         //here more code in order to save in a database
    }
}

as for the JSON you should start from a JObject since it's surrounded by { }, or remove them from around the JSON you posted in your question

like image 131
csharpwinphonexaml Avatar answered Oct 04 '22 17:10

csharpwinphonexaml