如何使用该属性作为JOLT的Value输入?

时间:2018-06-14 15:26:16

标签: jolt

对于我正在构建的特定函数,我需要解析我的JSON并在某些情况下将属性(而不是值本身)用作属性的值。但是我如何使用JOLT来管理它?

让我们说这是我的意见:

{
  "Results": [
    {
      "Name": "FirstName",
      "Value": "John"
    },
    {
      "Name": "FirstName",
      "Value": "Mary"
    },
    {
      "Name": "FirstName",
      "Value": "Thomas"
    },
    {
      "Name": "LastName",
      "Value": "Doe"
    },
    {
      "Name": "LastName",
      "Value": "Doe"
    },
    {
      "Name": "LastName",
      "Value": "Edison"
    },
  ]
}

这应该是结果:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:cropToPadding="false"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/ic_launcher_background" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Bold Text"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />
        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

对于那些感兴趣的人..我正在Mendix中构建一个JSON到Excel的导出功能,它必须是完全动态的,无论输入如何。为了实现这一点,我需要一个数组,其中每个属性(等于Excel中的一列)必须是它自己的对象,具有列名和值。如果每个列数据都是它自己的对象,我可以简单地说“为每个具有相同”名称的对象创建列。有点难以解释,但它“应该”有效。

1 个答案:

答案 0 :(得分:2)

Arrays和Jolt,不是最好的。基本上有3种方法可以在Shift中处理数组。

  1. 您明确地将数据分配给数组位置。 Aka foo[0]foo[1]
  2. 你引用了一个&#34;数字&#34;存在于输入数据中。 Aka foo[&2]foo[&3]
  3. 你&#34;积累&#34;数据到列表中。阿卡foo[]
  4. 您的输入数据是大小为3的数组。您想要的输出是一个大小为6的数组。您希望它具有灵活性并能够处理变量输入。

    这意味着选项3.所以你必须&#34;修复&#34; /处理您的数据&#34;最终形式&#34;,同时保持原始输入Json结构(具有3个项目的列表),然后累积所有&#34;内置&#34;将项目列入清单。

    这意味着你正在构建一个列表列表,然后最终&#34;压缩&#34;它归结为一个列表。

    规格

    [
      {
        // Step 1 : Pivot the data into parallel lists of keys and values
        //  maintaining the original outer input list structure.
        "operation": "shift",
        "spec": {
          "Results": {
            "*": { // results index
              "*": { // FirstName / Lastname
                "$": "temp[&2].keys[]",
                "@": "temp[&2].values[]"
              }
            }
          }
        }
      },
      {
        // Step 2 : Un-pivot the data into the desired
        //  Name/Value pairs, using the inner array index to 
        //  keep things organized/separated.
        "operation": "shift",
        "spec": {
          "temp": {
            "*": { // temp index
              "keys": {
                "*": "temp[&2].[&].Name"
              },
              "values": {
                "*": "temp[&2].[&].Value"
              }
            }
          }
        }
      },
      {
        // Step 3 : Accumulate the "finished" Name/Value pairs
        //  into the final "one big list" output.
        "operation": "shift",
        "spec": {
          "temp": {
            "*": { // outer array
              "*": "Results[]"
            }
          }
        }
      }
    ]