使用JSON JArray获得价值 [英] Get Value from JSON using JArray

查看:143
本文介绍了使用JSON JArray获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串(JSON格式)结果
我从服务器获得:

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.

有关,我尝试做如下:

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.

推荐答案

使用的ToString(),而不是的ToString

的ToString()是一个方法调用; 的ToString 的ToString 方法的引用,只能被分配到一个兼容的委托。

ToString() is a method call; ToString is a reference to the ToString method, and can only be assigned to a compatible delegate.

您也可以转换为字符串,因为 JToken 类定义的转换

You can also cast to String, since the JToken class defines a conversion:

string tempValue = (string)prop.Value;

要考虑的另一个选择是使用JSON序列:创建表示JSON数据的类(用相同的结构),和反序列化的JSON此类。它使代码更具有可读性和可维护性。

Another option to consider is to use JSON serialization: create a class that represents the JSON data (with the same structure), and deserialize the JSON to this class. It makes the code much more readable and maintainable.

这篇关于使用JSON JArray获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆