【cue语言系列】03.json marshal和unmarshal

需求

初始有一个json
需要cue文件来帮助拼接一段形成一个新的json

简单版

image.png

import (
  "encoding/json"
)

// 原json Marshal之后的字符串
a: "{\"test1\":\"1\",\"test2\":\"2\"}"

// step1: 先unmarshal
b: json.Unmarshal(a)

// step2:拼接新的内容
c: {
    b
    "testNew": "new"   // 新内容
}

// step3: 转成字符串
cMarshal: json.Marshal(c)


复杂版

image.png

import (
  "encoding/json"
)

// 原json Marshal之后的字符串
a: "{\"test1\":\"1\",\"test2\":\"2\"}"

// step1: 先unmarshal
b: json.Unmarshal(a)

testValue:  {
    "111": "111"
    "222": "222"
}

// step2:拼接新的内容
c: {
    b
    "testNew": testValue  // 新内容
}

// step3: 转成字符串
cMarshal: json.Marshal(c)

进阶版2:一级拼接

image.png

import (
  "encoding/json"
)

// 原json Marshal之后的字符串
a: "{\"test1\":\"1\",\"test2\":\"2\"}"

// step1: 先unmarshal
b: json.Unmarshal(a)

testValue:  {
    "111": "111"
    "222": "222"
    "333": b.test2
}

// step2:拼接新的内容
c: {
    b
    "testNew": testValue  // 新内容
}

// step3: 转成字符串
cMarshal: json.Marshal(c)

进阶版2的另一种写法

image.png

import (
  "encoding/json"
)

self: {
    // step1: 先unmarshal
    b: json.Unmarshal("\(global.a)")
    
    testValue:  {
        "111": "111"
        "222": "222"
        "333": b.test2
    }
    
    // step2:拼接新的内容
    c: {
        b
        "testNew": testValue  // 新内容
    }
}

global: {
    // 原json Marshal之后的字符串
    a: "{\"test1\":\"1\",\"test2\":\"2\"}"
    
    
    // step3: 转成字符串
    cMarshal: json.Marshal(self.c)
}

进阶版3:两级拼接

image.png

import (
  "encoding/json"
)

// 原json Marshal之后的字符串
a: "{\"test1\":\"1\",\"test2\":\"2\"}"
a2: "{\"test1bbbb\":\"1\",\"test2bbbb\":\"2\"}"

// step1: 先unmarshal
b: json.Unmarshal(a)
b2: json.Unmarshal(a2)

testValue:  {
    "111": "111"
    "222": "222"
    "333": b.test2
    b2
}

// step2:拼接新的内容
c: {
    b
    "testNew": testValue  // 新内容
}

// step3: 转成字符串
cMarshal: json.Marshal(c)

进阶版3的另一种写法

image.png

import (
  "encoding/json"
)

self: {
    // step1: 先unmarshal
    b: json.Unmarshal("\(global.a)")
    b2: json.Unmarshal("\(global.a2)")
    
    testValue:  {
        b2
        "111": "111"
        "222": "222"
        "333": b.test2
    }
    
    // step2:拼接新的内容
    c: {
        b
        "testNew": testValue  // 新内容
    }
}


global: {
    // 原json Marshal之后的字符串
    a: "{\"test1\":\"1\",\"test2\":\"2\"}"
    a2: "{\"test1bbbb\":\"1\",\"test2bbbb\":\"2\"}"
    
    
    // step3: 转成字符串
    cMarshal: json.Marshal(self.c)
    
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安安csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值