我遇到以下情况:
我有XML,它们包含base64编码的条目。解码它们似乎不是一个真正的问题(稍后会详细介绍)。
解码后的字符串不是UTF-8,但显然是USC2-Internal,可以使用iconv
进行转换。
现在我想进行更改并返回XML,用转换后的数据替换节点文本条目。
麻烦开始的地方(?)。
问题似乎是base64解码在开头留下了一个无效的5字节集。因此,即使是简单的步骤,也可以将base64解码为字符串,字符串到base64也不起作用。我尝试了几种使用openssl,python等方法。我在这里找到的所有建议都是如此。
无论如何 - 要转换的iconv
命令确实有效。
但是,如果不是“丑陋的字节”,那么再次改回USC-2-Internal将会失败。被忽略了。
这里有谁可以解释这些问题? 我可以发布一个例子(只是base64字符串)。
感谢。
以下是XML值的相关部分:
5gYAAAAAAAB7ACIAbQBUAGUAeAB0AFAAYQByAGEAbQAiADoAewAiAG0AQQBsAGkAZwBuAG0AZQBuAHQAIgA6ADIALAAiAG0ARABlAGYAYQB1AGwAdABSAHUAbgAiADoAWwBdACwAIgBtAEgAZQBpAGcAaAB0ACIAOgAxADIANAAsACIAbQBIAGkAbgBkAGkARABpAGcAaQB0AHMAIgA6AGYAYQBsAHMAZQAsACIAbQBJAG4AZABpAGMAIgA6AGYAYQBsAHMAZQAsACIAbQBJAHMAVgBlAHIAdABpAGMAYQBsAFQAZQB4AHQAIgA6AGYAYQBsAHMAZQAsACIAbQBMAGUAYQBkAGkAbgBnACIAOgAwACwAIgBtAEwAaQBnAGEAdAB1AHIAZQBzACIAOgBmAGEAbABzAGUALAAiAG0AUgBUAEwAIgA6AGYAYQBsAHMAZQAsACIAbQBTAGgAYQBkAG8AdwBBAG4AZwBsAGUAIgA6ADEAMwA1ACwAIgBtAFMAaABhAGQAbwB3AEIAbAB1AHIAIgA6ADAALAAiAG0AUwBoAGEAZABvAHcAQwBvAGwAbwByACIAOgAwACwAIgBtAFMAaABhAGQAbwB3AE8AZgBmAHMAZQB0ACIAOgAxADAALAAiAG0AUwBoAGEAZABvAHcATwBwAGEAYwBpAHQAeQAiADoAMQAwADAALAAiAG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AbABlACIAOgB7ACIAbQBQAGEAcgBhAG0AVgBhAGwAdQBlAHMAIgA6AFsAWwAwACwAZgBhAGwAcwBlAF0AXQB9ACwAIgBtAFMAdAByAG8AawBlAFcAaQBkAHQAaAAiADoAewAiAG0AUABhAHIAYQBtAFYAYQBsAHUAZQBzACIAOgBbAFsAMAAsADEAXQBdAH0ALAAiAG0AVABlAHgAdAAiADoAIgBTAG8AbQBlACAA9gB6AHUAZwD8AGwAIABzAHQAcgBhAN8AZQAuACIALAAiAG0AVAByAGEAYwBrAGkAbgBnACIAOgB7ACIAbQBQAGEAcgBhAG0AVgBhAGwAdQBlAHMAIgA6AFsAWwAwACwALQAyADQAXQBdAH0ALAAiAG0AVABzAHUAbQBpACIAOgB7ACIAbQBQAGEAcgBhAG0AVgBhAGwAdQBlAHMAIgA6AFsAWwAwACwAMABdAF0AfQB9ACwAIgBtAFQAYQBiAFcAaQBkAHQAaAAiADoANAAwADAALAAiAG0AVwBpAGQAdABoACIAOgAxADUAMwAzAH0ALAAiAG0AVgBlAHIAcwBpAG8AbgAiADoAMQB9AA==
对于我使用的脚本:
set convString to (do shell script "echo " & quoted form of convString & " | python -m base64 -d")
- >
Ê{"mTextParam":{"mAlignment":2,"mDefaultRun":[],"mHeight":124,"mHindiDigits":false,"mIndic":false,"mIsVerticalText":false,"mLeading":0,"mLigatures":false,"mRTL":false,"mShadowAngle":135,"mShadowBlur":0,"mShadowColor":0,"mShadowOffset":10,"mShadowOpacity":100,"mShadowVisible":false,"mStyleSheet":{"mBaselineShift":{"mParamValues":[[0,0]]},"mFillColor":{"mParamValues":[[0,16777215]]},"mFillOverStroke":{"mParamValues":[[0,true]]},"mFillVisible":{"mParamValues":[[0,true]]},"mFontName":{"mParamValues":[[0,"MinionPro-Regular"]]},"mFontSize":{"mParamValues":[[0,100]]},"mKerning":{"mParamValues":[[0,0]]},"mStrokeColor":{"mParamValues":[[0,16777215]]},"mStrokeVisible":{"mParamValues":[[0,false]]},"mStrokeWidth":{"mParamValues":[[0,1]]},"mText":"Some ˆzug¸l strafle.","mTracking":{"mParamValues":[[0,-24]]},"mTsumi":{"mParamValues":[[0,0]]}},"mTabWidth":400,"mWidth":1533},"mVersion":1}.
这个字符串是USC-2的味道
使其可读,尤其是mText
值,我使用iconv
来打开字符串的备份。
set convString to do shell script "echo | iconv -f UCS-2-INTERNAL -t utf-8 " & quoted form of buPath
- >
"ۦ{\"mTextParam\":{\"mAlignment\":2,\"mDefaultRun\":[],\"mHeight\":124,\"mHindiDigits\":false,\"mIndic\":false,\"mIsVerticalText\":false,\"mLeading\":0,\"mLigatures\":false,\"mRTL\":false,\"mShadowAngle\":135,\"mShadowBlur\":0,\"mShadowColor\":0,\"mShadowOffset\":10,\"mShadowOpacity\":100,\"mShadowVisible\":false,\"mStyleSheet\":{\"mBaselineShift\":{\"mParamValues\":[[0,0]]},\"mFillColor\":{\"mParamValues\":[[0,16777215]]},\"mFillOverStroke\":{\"mParamValues\":[[0,true]]},\"mFillVisible\":{\"mParamValues\":[[0,true]]},\"mFontName\":{\"mParamValues\":[[0,\"MinionPro-Regular\"]]},\"mFontSize\":{\"mParamValues\":[[0,100]]},\"mKerning\":{\"mParamValues\":[[0,0]]},\"mStrokeColor\":{\"mParamValues\":[[0,16777215]]},\"mStrokeVisible\":{\"mParamValues\":[[0,false]]},\"mStrokeWidth\":{\"mParamValues\":[[0,1]]},\"mText\":\"Some özugül straße.\",\"mTracking\":{\"mParamValues\":[[0,-24]]},\"mTsumi\":{\"mParamValues\":[[0,0]]}},\"mTabWidth\":400,\"mWidth\":1533},\"mVersion\":1}"
问题在于回归的方式
如上所述,即使第一个解码后的字符串也无法转换回base64。对于base64压缩,我尝试了openssl base64
它返回一个错误:
sh:-c:line0:寻找匹配时的意外EOF
希望这有助于使问题更清晰(或者我正在做的错误)。
昨天的答案已经消失了?!幸运的是我记住了步骤。
但是,只是为了扩展关于Murmel关于JSON的正确注释的原始问题。我没有JSON的经验,但从有关它的阅读中我感觉它并不太复杂。
是否有推荐的方式(对于我的工作流程)将JSON转换为' .plist' XML和返回。从阅读到目前为止,两者将涵盖或多或少相同的数据类型 - 对于我的工作流程(或问题)。
如果有办法,理想的工作流程:
一个简单的文字示例:
Arial Bold 。
至少会改变JSON(PLIST)值:
" mFontName" {" mParamValues":[[0," ArialMT"],[7," ArialMT粗体"] [8" ArialMT粗体&#34],[9" ArialMT粗体&#34],[10," ArialMT粗体"]]}。
" mText":" Arial Bold"。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>mFont</key><array><array><real>0</real><string>ArialMT</string></array><array><real>7</real><string>ArialMT-Bold</string></array><array><real>8</real><string>ArialMT-Bold</string></array><array><real>9</real><string>ArialMT-Bold</string></array><array><real>10</real><string>ArialMT-Bold</string></array></array><key>mText</key><string>Arial Bold</string></dict></plist>
如果我的编辑没有意义,请忽略。