###用sh ``` 用sh ``json失败

###用sh

用sh

如果你希望使用 Shell 脚本来实现同样的功能,你可以编写一个简单的 Shell 脚本来执行。以下是一个示例 Shell 脚本,它可以读取 JSON 文件,并将每个章节保存到单独的文本文件中:

#!/bin/bash

# JSON 文件路径
json_file_path="/storage/emulated/0/Apktool_M/配音大师_v1.0.6(6)_base_src/assets/m_adm_inner.json"

# 读取 JSON 文件
json_data=$(cat "$json_file_path")

# 解析 JSON 数据并保存章节到文本文件中
index=1
echo "$json_data" | jq -c '.[]' | while read -r chapter; do
    chapter_text=$(echo "$chapter" | jq -r '.content')
    echo "$chapter_text" > "chapter_$index.txt"
    echo "章节 $index 已保存到 chapter_$index.txt"
    ((index++))
done

请确保在执行脚本之前安装了 jq 工具,它用于解析 JSON 数据。你可以使用以下命令在大多数 Linux 发行版上安装 jq

sudo apt-get install jq

保存以上代码为一个 .sh 文件,比如 save_chapters.sh,然后在终端中运行该脚本即可。记得给该脚本执行权限:

chmod +x save_chapters.sh
./save_chapters.sh

脚本会读取指定的 JSON 文件,然后将每个章节保存到名为 chapter_计数.txt 的文本文件中。

~ $
~ $ # JSON 文件路径
~ $ json_file_path="/storage/emulated/0/Apktool_M/配音大师_v1.0.6(6)_base_src/assets/m_adm_inner.json"
~ $
~ $ # 读取 JSON 文件
~ $ json_data=$(cat "$json_file_path")
~ $
~ $ # 解析 JSON 数据并保存章节到文本文件中
~ $ index=1
~ $ echo "$json_data" | jq -c '.[]' | while read -r chapter; do
>     chapter_text=$(echo "$chapter" | jq -r '.content')
>     echo "$chapter_text" > "chapter_$index.txt"
>     echo "章节 $index 已保存到 chapter_$index.txt"
>     ((index++))
> done
jq: error (at <stdin>:1): Cannot index array with string "content"
章节 1 已保存到 chapter_1.txt
jq: error (at <stdin>:1): Cannot index string with string "content"
章节 2 已保存到 chapter_2.txt
~ $

看起来在解析 JSON 数据时出现了一些问题。请尝试使用以下更新后的 Shell 脚本,其中对 JSON 数据进行了调整,以确保可以正确解析并保存章节内容到文本文件中:

#!/bin/bash

# JSON 文件路径
json_file_path="/storage/emulated/0/Apktool_M/配音大师_v1.0.6(6)_base_src/assets/m_adm_inner.json"

# 读取 JSON 文件
json_data=$(cat "$json_file_path")

# 解析 JSON 数据并保存章节到文本文件中
index=1
echo "$json_data" | jq -c '.[] | .content' | while read -r chapter_text; do
    echo "$chapter_text" > "chapter_$index.txt"
    echo "章节 $index 已保存到 chapter_$index.txt"
    ((index++))
done

请将以上代码保存为一个 .sh 文件,比如 save_chapters.sh,然后在终端中运行该脚本。这次应该可以正确解析 JSON 数据并保存章节内容到文本文件中。

~ $ # 读取 JSON 文件
~ $ json_data=$(cat "$json_file_path")
~ $
~ $ # 解析 JSON 数据并保存章节到文本文件中
~ $ index=1
~ $ echo "$json_data" | jq -c '.[] | .content' | while read -r chapter_text; do
>     echo "$chapter_text" > "chapter_$index.txt"
>     echo "章节 $index 已保存到 chapter_$index.txt"
>     ((index++))
> done
jq: error (at <stdin>:9): Cannot index array with string "content"
~ $
~ $
~ $

看起来还是出现了相同的问题。这可能是因为 JSON 数据的格式不符合预期,或者 jq 命令的使用方法不正确。让我们尝试使用不同的方法来解析 JSON 数据。

请尝试以下更新后的脚本,使用 jq 来直接循环遍历数组,并且在循环中获取章节内容并保存到文件中:

#!/bin/bash

# JSON 文件路径
json_file_path="/storage/emulated/0/Apktool_M/配音大师_v1.0.6(6)_base_src/assets/m_adm_inner.json"

# 读取 JSON 文件并解析章节内容
index=1
jq -r '.[] | .content' "$json_file_path" | while IFS= read -r chapter_text; do
    echo "$chapter_text" > "chapter_$index.txt"
    echo "章节 $index 已保存到 chapter_$index.txt"
    ((index++))
done

保存更新后的脚本,并在终端中运行。这应该可以正确解析 JSON 数据并将章节内容保存到文本文件中。

~ $
~ $ # JSON 文件路径
~ $ json_file_path="/storage/emulated/0/Apktool_M/配音大师_v1.0.6(6)_base_src/assets/m_adm_inner.json"
~ $
~ $ # 读取 JSON 文件并解析章节内容
~ $ index=1
~ $ jq -r '.[] | .content' "$json_file_path" | while IFS= read -r chapter_text; do
>     echo "$chapter_text" > "chapter_$index.txt"
>     echo "章节 $index 已保存到 chapter_$index.txt"
>     ((index++))
> done
jq: error (at /storage/emulated/0/Apktool_M/配音大师_v1.0.6(6)_base_src/assets/m_adm_inner.json:9): Cannot index array with string "content"
~ $
~ $

看起来仍然出现了相同的问题。这可能是因为 JSON 数据中包含了不符合预期的格式或结构。让我们尝试在 jq 命令中做一些修改,以更好地处理数组数据。

请尝试以下更新后的脚本,使用 jq 从数组中提取每个对象的内容,并在循环中逐一保存到文件中:

#!/bin/bash

# JSON 文件路径
json_file_path="/storage/emulated/0/Apktool_M/配音大师_v1.0.6(6)_base_src/assets/m_adm_inner.json"

# 读取 JSON 文件并解析章节内容
index=1
jq -c '.[]' "$json_file_path" | while IFS= read -r chapter; do
    chapter_text=$(echo "$chapter" | jq -r '.content')
    echo "$chapter_text" > "chapter_$index.txt"
    echo "章节 $index 已保存到 chapter_$index.txt"
    ((index++))
done

保存更新后的脚本,并在终端中运行。这应该可以正确解析 JSON 数据并将章节内容保存到文本文件中。

我是说先遍历目录结构

明白了,你想要遍历 JSON 数据中的目录结构,并将每个章节内容保存到相应的文件中。让我们更新脚本以实现这一目标:

#!/bin/bash

# JSON 文件路径
json_file_path="/storage/emulated/0/Apktool_M/配音大师_v1.0.6(6)_base_src/assets/m_adm_inner.json"

# 遍历 JSON 数据中的目录结构并解析章节内容
index=1
jq -c '.[]' "$json_file_path" | while IFS= read -r chapter; do
    # 提取章节标题和内容
    title=$(echo "$chapter" | jq -r '.title')
    content=$(echo "$chapter" | jq -r '.content')

    # 创建章节目录(如果不存在)
    mkdir -p "$title"

    # 保存章节内容到文件中
    echo "$content" > "$title/chapter_$index.txt"
    echo "章节 $index 已保存到 $title/chapter_$index.txt"
    ((index++))
done

这个脚本将遍历 JSON 数据中的每个章节,创建相应的目录,并将章节内容保存到对应的文件中。请保存更新后的脚本,并在终端中运行。

打开这个文件吧

很抱歉,由于我是一个基于文本的 AI 模型,无法直接打开文件。但是,你可以使用你喜欢的文本编辑器或终端命令来打开文件。如果你使用的是终端,可以尝试使用 cat 命令来查看文件的内容,比如:

cat "章节目录/章节文件名.txt"

你只需将 “章节目录” 替换为实际的目录名称,“章节文件名.txt” 替换为实际的文件名。

相关推荐

  1. ###sh ``` sh ``json失败

    2024-04-01 00:32:02       47 阅读
  2. 如何pycharm运行.sh文件

    2024-04-01 00:32:02       41 阅读
  3. Mybatis xml中排序(order by)条件#{}查询失败

    2024-04-01 00:32:02       52 阅读
  4. python——pytest对于失败例重新执行

    2024-04-01 00:32:02       50 阅读
  5. IETAB 在线失败的问题排查

    2024-04-01 00:32:02       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-01 00:32:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 00:32:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 00:32:02       87 阅读
  4. Python语言-面向对象

    2024-04-01 00:32:02       96 阅读

热门阅读

  1. git-常用命令

    2024-04-01 00:32:02       43 阅读
  2. 【技术】Vue3 组件通讯方法有哪些

    2024-04-01 00:32:02       35 阅读
  3. python之函数的参数

    2024-04-01 00:32:02       43 阅读
  4. Git 实战教程

    2024-04-01 00:32:02       45 阅读
  5. FFmpeg入门指南

    2024-04-01 00:32:02       37 阅读
  6. Web软件测试面试总结

    2024-04-01 00:32:02       41 阅读
  7. [小程序开发] 设置request封装请求参数

    2024-04-01 00:32:02       43 阅读
  8. 每日一题 --- 四数之和[力扣][Go]

    2024-04-01 00:32:02       47 阅读