如何以递归方式检查 JSON 数组是否是对象或基元数组 [英] How do you check if a JSON array is an array of objects or primitives recursively

查看:57
本文介绍了如何以递归方式检查 JSON 数组是否是对象或基元数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题正是我的意思

Stackoverflow 上的所有问题都询问 json 数据是对象还是数组,但我要找的是看看我是否能找出数组是原始类型或对象的数组.

目前,我已经可以识别它是否是一个数组,只是如果它不是一个字符串数组,我将无法转换.

这段代码被包裹在一个 for 循环中,它的位置是 (var comArrEl in comArr),其中 comArr 是一个字符串数组.这个数组存储类似gesmes:Envelope:Cube:Cube:@currency="USD"

基本上,我们试图在这里编写一个通用的 API 包装器.

//判断是数组还是对象如果(令牌是 JArray){尝试{//将 comArrEl 解析为整数以进行索引访问if (int.TryParse(comArrEl, out int index)){//泵入数组var dataList = token.ToObject>();//是最后一个吗?如果(comArrEl!=最后){//让我们解决它//更新令牌if (index >= 0 && index < dataList.Count){token = JToken.Parse(JsonConvert.SerializeObject(dataList[index]));}}//是的,它是最后一个别的{var 属性 = dataList[index];//号码检查//确保我们定位的 datalist 元素包含正确的值.if (decimal.TryParse(property, out decimal val)){//更新它_currencyPairComponentService.UpdatePairValue(component.Id, val);}}}//错误的 comArrEl.别的{返回假;}}捕获(异常前){Console.WriteLine(ex);}}

如您所见,异常在第 2 行触发.

示例有效载荷代码:

<预><代码>[{@货币:美元","@rate": "1.1354"},{"@currency": "日元",@rate":128.31"},{"@currency": "BGN","@rate": "1.9558"},{"@currency": "捷克克朗","@rate": "25.886"},{"@currency": "丹麦克朗","@rate": "7.4630"},{"@currency": "英镑",@rate":0.88885"},{"@currency": "HUF","@rate": "323.49"},{"@currency": "PLN","@rate": "4.2826"},{"@currency": "罗恩","@rate": "4.6528"},{"@currency": "瑞典克朗","@rate": "10.1753"},{"@currency": "瑞士法郎","@rate": "1.1328"},{"@currency": "ISK",@rate":139.40"},{"@currency": "NOK","@rate": "9.6480"},{"@currency": "HRK","@rate": "7.3990"},{"@currency": "擦","@rate": "75.8385"},{"@currency": "尝试","@rate": "6.0453"},{"@currency": "澳元","@rate": "1.5569"},{"@currency": "巴西雷亚尔","@rate": "4.3692"},{"@currency": "加元","@rate": "1.5076"},{"@currency": "人民币","@rate": "7.7848"},{"@currency": "港币","@rate": "8.8695"},{"@currency": "印尼盾","@rate": "16344.08"},{"@currency": "ILS","@rate": "4.2293"},{"@currency": "印度卢比","@rate": "80.0660"},{"@currency": "韩元","@rate": "1264.39"},{"@currency": "MXN",@rate":23.2282"},{"@currency": "MYR","@rate": "4.7165"},{"@currency": "新西兰元","@rate": "1.6398"},{"@currency": "PHP",@rate":59.878"},{"@currency": "新加坡元","@rate": "1.5520"},{"@currency": "泰铢",@rate":37.190"},{"@currency": "ZAR","@rate": "15.6366"}]

解决方案

该方法可以动态迭代一个 JSON 结果,无论是对象还是原语.此方法循环遍历名为 requestComponents 的数组,该数组定义了我们希望从 JSON 负载中获取的属性.

然后它将每个 requestComponent 字符串分解为一个数组(即gesmes:Envelope/Cube/Cube/Cube/0=>@rate"),以允许我们向下遍历对象/数组.有时它是一个数组,有时它是一个对象,我们可以轻松地沿着这条路走下去.仅适用于 requestComponent 数组的最后一个元素的自定义=>"语法将告诉代码检索该属性.(sample: 0=>@currency//这将告诉我们获取索引为 0 的对象,即@currency"属性)

这是代码,不要关注输入,而是关注逻辑.这里的要点是在处理它之前查看即将到来的属性以查看它是否是数组或其他.所以你的目标可能是

object/array/object/array/array 元素索引

也可以是

数组/对象/数组/对象/属性

ResponseType 是一个枚举,RequestComponents 是一个包含要遍历的字符串的对象(这就是该对象中最重要的 tbh),而令牌基本上是 json 负载.

public bool Update(JToken token, ResponseType resType, IEnumerable requestComponents){//对于我们正在检查的每个组件foreach(requestComponents 中的 var 组件){var comArr = component.QueryComponent.Split("/");//如果字符串是嵌套的,则拆分字符串var last = comArr.LastOrDefault();//获取最后一个以识别是否是最后一个//迭代查询组件数组foreach(comArr 中的 var comArrEl){//空检查如果(comArrEl!= null){//检查电流类型//判断是数组还是对象如果(令牌是 JArray){尝试{//是最后一个吗?如果(comArrEl!=最后){//将 comArrEl 解析为整数以进行索引访问if (int.TryParse(comArrEl, out int index)){//泵入数组,将其视为匿名.var dataList = token.ToObject>();//让我们解决它//更新令牌if (index >= 0 && index < dataList.Count){//遍历数组token = JToken.Parse(JsonConvert.SerializeObject(dataList[index]));}}}//是的,它是最后一个别的{//查看是否有我们需要引用的属性.var comArrElArr = comArrEl.Split("=>");if (int.TryParse(comArrElArr[0], out var index)){//先遍历var rawData = token.ToObject>()[index];//如果它是 1,我们假设它只是一个原始类型的数组如果(comArrElArr.Length == 1){//检索值.var rawVal = rawData.ToString();//https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparsevar style = NumberStyles.Any;如果 (ExponentHelper.IsExponentialFormat(rawVal)){style = NumberStyles.Float;}//如果是指数如果(decimal.TryParse(rawVal,风格,CultureInfo.InvariantCulture,出 var val)){如果 (val > 0){//更新它_currencyPairComponentService.UpdatePairValue(component.Id, val);}}}//哦不...非原始...否则如果(comArrElArr.Length == 2){//对象化var rawObj = JObject.Parse(rawData.ToString());//获得想要的值var rawVal = rawObj[comArrElArr[1]].ToString();//像往常一样,更新它//https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparsevar style = NumberStyles.Any;如果 (ExponentHelper.IsExponentialFormat(rawVal)){style = NumberStyles.Float;}//如果是指数如果(decimal.TryParse(rawVal,风格,CultureInfo.InvariantCulture,出 var val)){如果 (val > 0){//更新它_currencyPairComponentService.UpdatePairValue(component.Id, val);}}}别的{//无效的返回假;}}}}捕获(异常前){Console.WriteLine(ex);}}else if(令牌是JObject){//泵入对象JObject obj = token.ToObject();//是最后一个吗?如果(comArrEl!=最后){//让我们解决它//更新令牌token = obj.SelectToken(comArrEl);}//是的,它是最后一个别的{//查看是否有我们需要引用的属性.var comArrElArr = comArrEl.Split("=>");//先遍历var rawData = (string) obj.SelectToken(comArrElArr[0]);如果(原始数据!= null){//如果它是 1,我们假设它只是一个原始类型的数组如果(comArrElArr.Length == 1){//检索值.var rawVal = rawData.ToString();//https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparsevar style = NumberStyles.Any;如果 (ExponentHelper.IsExponentialFormat(rawVal)){style = NumberStyles.Float;}//如果是指数如果(decimal.TryParse(rawVal,风格,CultureInfo.InvariantCulture,出 var val)){如果 (val > 0){//更新它_currencyPairComponentService.UpdatePairValue(component.Id, val);}}}//哦不...非原始...否则如果(comArrElArr.Length == 2){//对象化var rawObj = JObject.Parse(rawData.ToString());//获得想要的值var rawVal = rawObj[comArrElArr[1]].ToString();//像往常一样,更新它//https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparsevar style = NumberStyles.Any;如果 (ExponentHelper.IsExponentialFormat(rawVal)){style = NumberStyles.Float;}//如果是指数如果(decimal.TryParse(rawVal,风格,CultureInfo.InvariantCulture,出 var val)){如果 (val > 0){//更新它_currencyPairComponentService.UpdatePairValue(component.Id, val);}}}别的{//无效的返回假;}}}}//像 JObject 一样迭代 JValue否则如果(令牌是 JValue){//泵入对象JObject obj = token.ToObject();//是最后一个吗?如果(comArrEl!=最后){//让我们解决它//更新令牌token = obj.SelectToken(comArrEl);}//是的,它是最后一个别的{var rawData = (string) obj.SelectToken(component.QueryComponent);如果(原始数据!= null){//https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparsevar style = NumberStyles.Any;如果 (ExponentHelper.IsExponentialFormat(rawData)){style = NumberStyles.Float;}//如果是指数if (decimal.TryParse(rawData, style, CultureInfo.InvariantCulture,输出十进制值)){如果 (val > 0){//更新它_currencyPairComponentService.UpdatePairValue(component.Id, val);}}}}}}别的{//发生了不好的事情返回假;}}}返回假;}

什么类型的数据输入有效?

好吧,'token' 属性可以是这个

<代码>{?xml":{"@version": "1.0","@encoding": "UTF-8"},gesmes:信封":{"@xmlns:gesmes": "http://www.gesmes.org/xml/2002-08-01","@xmlns": "http://www.ecb.int/vocabulary/2002-08-01/eurofxref","gesmes:subject": "参考率",gesmes:发件人":{"gesmes:name": "欧洲中央银行"},立方体":{立方体":{"@time": "2018-12-06",立方体":[{@货币:美元","@rate": "1.1351"},{"@currency": "日元",@rate":128.04"},{"@currency": "BGN","@rate": "1.9558"},{"@currency": "捷克克朗",@rate":25.890"},{"@currency": "丹麦克朗","@rate": "7.4635"},{"@currency": "英镑",@rate":0.88930"},{"@currency": "HUF",@rate":323.75"},{"@currency": "PLN","@rate": "4.2881"},{"@currency": "罗恩","@rate": "4.6548"},{"@currency": "瑞典克朗","@rate": "10.2355"},{"@currency": "瑞士法郎","@rate": "1.1304"},{"@currency": "ISK","@rate": "138.40"},{"@currency": "NOK","@rate": "9.7028"},{"@currency": "HRK","@rate": "7.3965"},{"@currency": "擦",@rate":75.9421"},{"@currency": "尝试","@rate": "6.0947"},{"@currency": "澳元","@rate": "1.5745"},{"@currency": "巴西雷亚尔","@rate": "4.4370"},{"@currency": "加元","@rate": "1.5229"},{"@currency": "人民币","@rate": "7.8239"},{"@currency": "港币","@rate": "8.8669"},{"@currency": "印尼盾","@rate": "16481.65"},{"@currency": "ILS","@rate": "4.2367"},{"@currency": "印度卢比","@rate": "80.4950"},{"@currency": "韩元",@rate":1273.03"},{"@currency": "MXN","@rate": "23.3643"},{"@currency": "MYR","@rate": "4.7271"},{"@currency": "新西兰元","@rate": "1.6517"},{"@currency": "PHP",@rate":60.012"},{"@currency": "新加坡元","@rate": "1.5560"},{"@currency": "泰铢",@rate":37.282"},{"@currency": "ZAR","@rate": "15.9797"}]}}}}

更新方法:需要第 6 行的样本输入吗?

这将获得@rate 属性.

gesmes:Envelope/Cube/Cube/Cube/0=>@rate

更简单的示例:

<预><代码>[97.935,1745.41815607,97.936,315.65150703,-6.58,-0.063,97.93,664801.20661302,105.67,96.23]

第 6 行就是0"

The title is exactly what I meant

All the questions on Stackoverflow asks for whether if the json data is an object or array but what I'm looking for is to see if I can find out if the array is an array of primitive types or objects.

Currently, i already can identify if its an array or not, just that I'm unable to convert if it is not an array of strings.

This code is wrapped in a for loop, where it is (var comArrEl in comArr), where comArr is an array of strings. This array stores something like "gesmes:Envelope:Cube:Cube:@currency="USD"

Basically we're trying to write a universal API wrapper here.

// Identify if its an array or an object
if (token is JArray)
{
    try
    {
        // Parse the comArrEl to an integer for index access
        if (int.TryParse(comArrEl, out int index))
        {
            // Pump in the array
            var dataList = token.ToObject<List<object>>();

            // Is it the last?
            if (comArrEl != last)
            {
                // let's work it out
                // update the token
                if (index >= 0 && index < dataList.Count)
                {
                    token = JToken.Parse(JsonConvert.SerializeObject(dataList[index]));
                }
            }
            // Yes its the last
            else
            {
                var property = dataList[index];

                // Number checks
                // Make sure the datalist element we're targetting contains a proper value.
                if (decimal.TryParse(property, out decimal val))
                {
                    // Update it
                    _currencyPairComponentService.UpdatePairValue(component.Id, val);
                }
            }
        }
        // Incorrect comArrEl.
        else
        {
            return false;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

As you can see, the exception triggers on line 2.

Sample payload to code:

[
    {
        "@currency": "USD",
        "@rate": "1.1354"
    },
    {
        "@currency": "JPY",
        "@rate": "128.31"
    },
    {
        "@currency": "BGN",
        "@rate": "1.9558"
    },
    {
        "@currency": "CZK",
        "@rate": "25.886"
    },
    {
        "@currency": "DKK",
        "@rate": "7.4630"
    },
    {
        "@currency": "GBP",
        "@rate": "0.88885"
    },
    {
        "@currency": "HUF",
        "@rate": "323.49"
    },
    {
        "@currency": "PLN",
        "@rate": "4.2826"
    },
    {
        "@currency": "RON",
        "@rate": "4.6528"
    },
    {
        "@currency": "SEK",
        "@rate": "10.1753"
    },
    {
        "@currency": "CHF",
        "@rate": "1.1328"
    },
    {
        "@currency": "ISK",
        "@rate": "139.40"
    },
    {
        "@currency": "NOK",
        "@rate": "9.6480"
    },
    {
        "@currency": "HRK",
        "@rate": "7.3990"
    },
    {
        "@currency": "RUB",
        "@rate": "75.8385"
    },
    {
        "@currency": "TRY",
        "@rate": "6.0453"
    },
    {
        "@currency": "AUD",
        "@rate": "1.5569"
    },
    {
        "@currency": "BRL",
        "@rate": "4.3692"
    },
    {
        "@currency": "CAD",
        "@rate": "1.5076"
    },
    {
        "@currency": "CNY",
        "@rate": "7.7848"
    },
    {
        "@currency": "HKD",
        "@rate": "8.8695"
    },
    {
        "@currency": "IDR",
        "@rate": "16344.08"
    },
    {
        "@currency": "ILS",
        "@rate": "4.2293"
    },
    {
        "@currency": "INR",
        "@rate": "80.0660"
    },
    {
        "@currency": "KRW",
        "@rate": "1264.39"
    },
    {
        "@currency": "MXN",
        "@rate": "23.2282"
    },
    {
        "@currency": "MYR",
        "@rate": "4.7165"
    },
    {
        "@currency": "NZD",
        "@rate": "1.6398"
    },
    {
        "@currency": "PHP",
        "@rate": "59.878"
    },
    {
        "@currency": "SGD",
        "@rate": "1.5520"
    },
    {
        "@currency": "THB",
        "@rate": "37.190"
    },
    {
        "@currency": "ZAR",
        "@rate": "15.6366"
    }
]

解决方案

This method can dynamically iterate a JSON result, be it object or primitive. This method loops through an array called requestComponents , which defines the properties we want to obtain from the JSON payload.

It then breaks down each requestComponent string into an array (i.e. "gesmes:Envelope/Cube/Cube/Cube/0=>@rate") to allow us to go down the object/array. Sometimes its an array, sometimes its an object, we can easily go down that path. A custom "=>" syntax that only works for the last element of the requestComponent array will tell the code to retrieve that property. (sample: 0=>@currency // That will tell us to obtain the object of index 0, the "@currency" property)

Here's the code, do not focus on the inputs, but rather the logic. The main point here is to peek into the upcoming property to see if its an array or etc before processing it. So your target could be

object/array/object/array/array element index

or it could also be

array/object/array/object/property

voila

ResponseType is an Enum, RequestComponents is an object that contains the string to traverse (That's all that matters in that object tbh) and token is basically the json payload.

public bool Update(JToken token, ResponseType resType, IEnumerable<RequestComponent> requestComponents)
{
    // For each component we're checking
    foreach (var component in requestComponents)
    {
        var comArr = component.QueryComponent.Split("/"); // Split the string if its nesting
        var last = comArr.LastOrDefault(); // get the last to identify if its the last

        // Iterate the queryComponent Array
        foreach (var comArrEl in comArr)
        {
            // Null check
            if (comArrEl != null)
            {
                // CHECK CURRENT TYPE
                // Identify if its an array or an object
                if (token is JArray)
                {
                    try
                    {
                        // Is it the last?
                        if (comArrEl != last)
                        {
                            // Parse the comArrEl to an integer for index access
                            if (int.TryParse(comArrEl, out int index))
                            {
                                // Pump in the array, treat it as anonymous.
                                var dataList = token.ToObject<List<JObject>>();

                                // let's work it out
                                // update the token
                                if (index >= 0 && index < dataList.Count)
                                {
                                    // Traverse the array
                                    token = JToken.Parse(JsonConvert.SerializeObject(dataList[index]));
                                }
                            }
                        }
                        // Yes its the last
                        else
                        {
                            // See if theres any property we need to refer to.
                            var comArrElArr = comArrEl.Split("=>");

                            if (int.TryParse(comArrElArr[0], out var index))
                            {
                                // Traverse first
                                var rawData = token.ToObject<List<JToken>>()[index];

                                // if its 1, we assume its just an array of a primitive type
                                if (comArrElArr.Length == 1)
                                {
                                    // Retrieve the value.
                                    var rawVal = rawData.ToString();

                                    // https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse
                                    var style = NumberStyles.Any;
                                    if (ExponentHelper.IsExponentialFormat(rawVal))
                                    {
                                        style = NumberStyles.Float;
                                    }

                                    // If it is an exponent
                                    if (decimal.TryParse(rawVal, style, CultureInfo.InvariantCulture,
                                        out var val))
                                    {
                                        if (val > 0)
                                        {
                                            // Update it
                                            _currencyPairComponentService.UpdatePairValue(component.Id, val);
                                        }
                                    }
                                }
                                // Oh no.. non-primitive...
                                else if (comArrElArr.Length == 2)
                                {
                                    // Object-ify
                                    var rawObj = JObject.Parse(rawData.ToString());

                                    // Obtain the desired value
                                    var rawVal = rawObj[comArrElArr[1]].ToString();

                                    // As usual, update it
                                    // https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse
                                    var style = NumberStyles.Any;
                                    if (ExponentHelper.IsExponentialFormat(rawVal))
                                    {
                                        style = NumberStyles.Float;
                                    }

                                    // If it is an exponent
                                    if (decimal.TryParse(rawVal, style, CultureInfo.InvariantCulture,
                                        out var val))
                                    {
                                        if (val > 0)
                                        {
                                            // Update it
                                            _currencyPairComponentService.UpdatePairValue(component.Id, val);
                                        }
                                    }
                                }
                                else
                                {
                                    // Invalid
                                    return false;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
                else if (token is JObject)
                {
                    // Pump in the object
                    JObject obj = token.ToObject<JObject>();

                    // Is it the last?
                    if (comArrEl != last)
                    {
                        // let's work it out
                        // update the token
                        token = obj.SelectToken(comArrEl);
                    }
                    // Yes its the last
                    else
                    {
                        // See if theres any property we need to refer to.
                        var comArrElArr = comArrEl.Split("=>");

                        // Traverse first
                        var rawData = (string) obj.SelectToken(comArrElArr[0]);

                        if (rawData != null)
                        {
                            // if its 1, we assume its just an array of a primitive type
                            if (comArrElArr.Length == 1)
                            {
                                // Retrieve the value.
                                var rawVal = rawData.ToString();

                                // https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse
                                var style = NumberStyles.Any;
                                if (ExponentHelper.IsExponentialFormat(rawVal))
                                {
                                    style = NumberStyles.Float;
                                }

                                // If it is an exponent
                                if (decimal.TryParse(rawVal, style, CultureInfo.InvariantCulture,
                                    out var val))
                                {
                                    if (val > 0)
                                    {
                                        // Update it
                                        _currencyPairComponentService.UpdatePairValue(component.Id, val);
                                    }
                                }
                            }
                            // Oh no.. non-primitive...
                            else if (comArrElArr.Length == 2)
                            {
                                // Object-ify
                                var rawObj = JObject.Parse(rawData.ToString());

                                // Obtain the desired value
                                var rawVal = rawObj[comArrElArr[1]].ToString();

                                // As usual, update it
                                // https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse
                                var style = NumberStyles.Any;
                                if (ExponentHelper.IsExponentialFormat(rawVal))
                                {
                                    style = NumberStyles.Float;
                                }

                                // If it is an exponent
                                if (decimal.TryParse(rawVal, style, CultureInfo.InvariantCulture,
                                    out var val))
                                {
                                    if (val > 0)
                                    {
                                        // Update it
                                        _currencyPairComponentService.UpdatePairValue(component.Id, val);
                                    }
                                }
                            }
                            else
                            {
                                // Invalid
                                return false;
                            }
                        }
                    }
                }
                // iterate JValue like a JObject
                else if (token is JValue)
                {
                    // Pump in the object
                    JObject obj = token.ToObject<JObject>();

                    // Is it the last?
                    if (comArrEl != last)
                    {
                        // let's work it out
                        // update the token
                        token = obj.SelectToken(comArrEl);
                    }
                    // Yes its the last
                    else
                    {
                        var rawData = (string) obj.SelectToken(component.QueryComponent);

                        if (rawData != null)
                        {
                            // https://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse
                            var style = NumberStyles.Any;
                            if (ExponentHelper.IsExponentialFormat(rawData))
                            {
                                style = NumberStyles.Float;
                            }

                            // If it is an exponent
                            if (decimal.TryParse(rawData, style, CultureInfo.InvariantCulture,
                                out decimal val))
                            {
                                if (val > 0)
                                {
                                    // Update it
                                    _currencyPairComponentService.UpdatePairValue(component.Id, val);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                // Something bad happened
                return false;
            }
        }
    }

    return false;
}

What type of data input works?

well, the 'token' property can be this

{
  "?xml": {
    "@version": "1.0",
    "@encoding": "UTF-8"
  },
  "gesmes:Envelope": {
    "@xmlns:gesmes": "http://www.gesmes.org/xml/2002-08-01",
    "@xmlns": "http://www.ecb.int/vocabulary/2002-08-01/eurofxref",
    "gesmes:subject": "Reference rates",
    "gesmes:Sender": {
      "gesmes:name": "European Central Bank"
    },
    "Cube": {
      "Cube": {
        "@time": "2018-12-06",
        "Cube": [
          {
            "@currency": "USD",
            "@rate": "1.1351"
          },
          {
            "@currency": "JPY",
            "@rate": "128.04"
          },
          {
            "@currency": "BGN",
            "@rate": "1.9558"
          },
          {
            "@currency": "CZK",
            "@rate": "25.890"
          },
          {
            "@currency": "DKK",
            "@rate": "7.4635"
          },
          {
            "@currency": "GBP",
            "@rate": "0.88930"
          },
          {
            "@currency": "HUF",
            "@rate": "323.75"
          },
          {
            "@currency": "PLN",
            "@rate": "4.2881"
          },
          {
            "@currency": "RON",
            "@rate": "4.6548"
          },
          {
            "@currency": "SEK",
            "@rate": "10.2355"
          },
          {
            "@currency": "CHF",
            "@rate": "1.1304"
          },
          {
            "@currency": "ISK",
            "@rate": "138.40"
          },
          {
            "@currency": "NOK",
            "@rate": "9.7028"
          },
          {
            "@currency": "HRK",
            "@rate": "7.3965"
          },
          {
            "@currency": "RUB",
            "@rate": "75.9421"
          },
          {
            "@currency": "TRY",
            "@rate": "6.0947"
          },
          {
            "@currency": "AUD",
            "@rate": "1.5745"
          },
          {
            "@currency": "BRL",
            "@rate": "4.4370"
          },
          {
            "@currency": "CAD",
            "@rate": "1.5229"
          },
          {
            "@currency": "CNY",
            "@rate": "7.8239"
          },
          {
            "@currency": "HKD",
            "@rate": "8.8669"
          },
          {
            "@currency": "IDR",
            "@rate": "16481.65"
          },
          {
            "@currency": "ILS",
            "@rate": "4.2367"
          },
          {
            "@currency": "INR",
            "@rate": "80.4950"
          },
          {
            "@currency": "KRW",
            "@rate": "1273.03"
          },
          {
            "@currency": "MXN",
            "@rate": "23.3643"
          },
          {
            "@currency": "MYR",
            "@rate": "4.7271"
          },
          {
            "@currency": "NZD",
            "@rate": "1.6517"
          },
          {
            "@currency": "PHP",
            "@rate": "60.012"
          },
          {
            "@currency": "SGD",
            "@rate": "1.5560"
          },
          {
            "@currency": "THB",
            "@rate": "37.282"
          },
          {
            "@currency": "ZAR",
            "@rate": "15.9797"
          }
        ]
      }
    }
  }
}

Update method: need a sample input for line 6?

This will obtain the @rate property.

gesmes:Envelope/Cube/Cube/Cube/0=>@rate

Easier sample:

[
  97.935,
  1745.41815607,
  97.936,
  315.65150703,
  -6.58,
  -0.063,
  97.93,
  664801.20661302,
  105.67,
  96.23
]

Line 6 would be just "0"

这篇关于如何以递归方式检查 JSON 数组是否是对象或基元数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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