search
HomeBackend DevelopmentGolangHow to remove unwanted characters between keys and values ​​in JSON?

How to remove unwanted characters between keys and values ​​in JSON?

php editor Strawberry is here to share with you a tip about JSON processing: How to delete unnecessary characters between keys and values ​​in JSON? When processing JSON data, we often need to clean up some useless characters to improve the readability of the data and reduce the size of data transmission. By using PHP's related functions and regular expressions, we can easily remove unnecessary characters between keys and values ​​in JSON, making the data more tidy and standardized. Next, I will introduce the specific operation steps and code implementation in detail.

Question content

I have this JSON: {"key1": "value1", \n \n "key2": "value2\nwithnewline"}

I want:

  1. Delete\n\n
  2. Retain value2\n and newline characters.

So I will have a valid JSON.

I tried :regex but couldn't figure out how to specify outside of keys and values.

and this:

package main

import (
    "bytes"
    "fmt"
)

func main() {
    jsonStr := `{"key1": "value1", \n \n "key2": "value2\nwithnewline"}`
    var cleaned bytes.Buffer
    quoteCount := 0

    for i := 0; i < len(jsonStr); i++ {
        if jsonStr[i] == '"' {
            quoteCount++
        }

        if quoteCount%2 == 0 && jsonStr[i] != '\n' {
            cleaned.WriteByte(jsonStr[i])
        } else if quoteCount%2 == 1 {
            cleaned.WriteByte(jsonStr[i])
        }
    }

    fmt.Println(cleaned.String())
}

Go playground link: https://go.dev/play/p/zvNSCuE4SjQ

This doesn't work as it could \n Actually it does \ n

Workaround

Given the parameters of your question, you can use strings.ReplaceAll to replace all\n\n:

cleaned := strings.ReplaceAll(input, "\\n \\n ", "")

If you want to continue using the current strategy, you will encounter some problems. One of them is that regardless of your condition, you always write the string: cleaned.WriteByte(jsonStr[i]) happens in your if and else cases.

The above is the detailed content of How to remove unwanted characters between keys and values ​​in JSON?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete
PHP和MySQL如何处理JSON中的特殊字符和转义符号?PHP和MySQL如何处理JSON中的特殊字符和转义符号?Jul 13, 2023 pm 10:29 PM

PHP和MySQL如何处理JSON中的特殊字符和转义符号?随着互联网的快速发展,JSON(JavaScriptObjectNotation)作为一种轻量级的数据交换格式,被广泛应用于前后端数据交互。在PHP和MySQL中,处理JSON数据的能力也变得越来越重要。然而,有时候JSON数据中会包含特殊字符和转义符号,这可能导致数据不正确地解析和显示。在本文中

如何使用PHP和MySQL处理JSON数组?如何使用PHP和MySQL处理JSON数组?Jul 14, 2023 pm 07:13 PM

如何使用PHP和MySQL处理JSON数组?简介:在现代的Web应用程序开发中,处理JSON数据已经成为常见的需求。PHP和MySQL是广泛使用的技术,了解如何使用它们来处理JSON数组将会大大提升开发效率和灵活性。本文将介绍如何使用PHP和MySQL处理JSON数组,并提供相应的代码示例。一、创建数据库表格在使用PHP和MySQL处理JSON数组之前,我们

如何删除JSON中键和值之间不需要的字符?如何删除JSON中键和值之间不需要的字符?Feb 09, 2024 pm 05:15 PM

我有这样的JSON:{"key1":"value1",\n\n"key2":"value2\nwithnewline"}我想要:删除\n\n保留value2\n和换行符。所以我将拥有一个有效的JSON。我尝试过:regex,但未能弄清楚如何在键和值之外指定。还有这个:packagemainimport("bytes""fmt")funcmain(){jsonStr:=`{"key1":"value1",\n\n

征服 Java JSON 处理的巅峰:解析和创建复杂数据征服 Java JSON 处理的巅峰:解析和创建复杂数据Mar 09, 2024 am 09:13 AM

解析JSON数据解析JSON数据是处理复杂数据的关键一步。在Java中,我们可以使用以下方法:使用Gson库:Gson是一个广泛使用的jsON解析库,提供了一个简洁且高效的api,如下所示:Gsongson=newGson();JsonObjectjsonObject=gson.fromJson(jsonString,JsonObject.class);使用Jackson库:Jackson是另一个流行的JSON处理库,支持丰富的功能和对其他格式(如XML)的转换,如下所示:ObjectMappe

如何使用PHP和MySQL处理JSON中的数字和浮点数?如何使用PHP和MySQL处理JSON中的数字和浮点数?Jul 13, 2023 pm 01:06 PM

如何使用PHP和MySQL处理JSON中的数字和浮点数?在现代的Web开发中,数据的传输和存储通常使用JSON格式。JSON是一种轻量级的数据交换格式,广泛应用于前后端数据传递和API接口中。在处理JSON数据时,经常会遇到对数字和浮点数的处理需求。本文将讲解如何使用PHP和MySQL来处理JSON中的数字和浮点数。从JSON解析出数字或浮点数在PHP中,可

PHP API开发中的如何处理XML和JSON格式数据PHP API开发中的如何处理XML和JSON格式数据Jun 17, 2023 pm 06:29 PM

在现代软件开发中,许多应用程序都需要通过API(应用程序接口)进行交互,允许不同的应用程序之间进行数据共享和通信。在PHP开发中,API是一种常见的技术,让PHP开发人员能够与其他系统集成,并使用不同的数据格式。在本文中,我们将探讨如何在PHPAPI开发中处理XML和JSON格式数据。XML格式数据处理XML(可扩展标记语言)是一种常用的数据格式,用于在不

深入理解Java开发中的JSON处理技巧深入理解Java开发中的JSON处理技巧Nov 20, 2023 pm 01:17 PM

深入理解Java开发中的JSON处理技巧摘要:随着互联网的发展和数据交互的广泛应用,处理JSON数据已成为现代软件开发中不可或缺的一部分。本文将深入探讨Java开发中的JSON处理技巧,包括JSON的基本概念、使用JSON的好处以及在Java开发中常用的JSON处理工具。一、JSON的基本概念JSON(JavaScriptObjectNotation)是

如何使用PHP和MySQL处理复杂的JSON数据结构?如何使用PHP和MySQL处理复杂的JSON数据结构?Jul 12, 2023 pm 02:55 PM

如何使用PHP和MySQL处理复杂的JSON数据结构?在现代的Web开发中,JSON(JavaScriptObjectNotation)已经成为交换数据的主要格式之一。当处理复杂的JSON数据结构时,PHP和MySQL提供了强大的工具和功能来帮助我们有效地处理和操作数据。本文将介绍如何使用PHP和MySQL来处理和操作复杂的JSON数据结构,并提供代码示

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool