実現したいこと
VoicevoxWebAPI
VoicevoxをWebAPIで実行して結果を取得したいのですが
stringからJSONに変換した時点で変数に格納された値が消えてしまいます。
(日本語全角文字とURL部分)
WebAPIからのレスポンス
{"success":true,"isApiKeyValid":false,"speakerName":"VOICEVOX:\u56db\u56fd\u3081\u305f\u3093\uff08\u3042\u307e\u3042\u307e\uff09","audioStatusUrl":"https:\/\/audio1.tts.quest\/v1\/data\/ebf199a7038db805a9840e6a2cd5b630c6849d95fd32ff8c99d8a0940ae6dec6\/status.json","wavDownloadUrl":"https:\/\/audio1.tts.quest\/v1\/data\/ebf199a7038db805a9840e6a2cd5b630c6849d95fd32ff8c99d8a0940ae6dec6\/audio.wav","mp3DownloadUrl":"https:\/\/audio1.tts.quest\/v1\/data\/ebf199a7038db805a9840e6a2cd5b630c6849d95fd32ff8c99d8a0940ae6dec6\/audio.mp3","mp3StreamingUrl":"https:\/\/audio1.tts.quest\/v1\/data\/ebf199a7038db805a9840e6a2cd5b630c6849d95fd32ff8c99d8a0940ae6dec6\/audio.mp3s"}
そのままJsonにデシリアライズした結果
C#
1success:True 2isApiKeyValid:False 3speakerName: 4audioStatusUrl: 5wavDownloadUrl: 6mp3DownloadUrl: 7mp3StreaminUrl:
発生している問題・分からないこと
WebAPIからのレスポンスに\uがついていたのでUnicodeだと思い変換したのですが変化がありませんでした
変換前
"speakerName":"VOICEVOX:\u56db\u56fd\u3081\u305f\u3093\uff08\u3042\u307e\u3042\u307e\uff09",
UnicodeからUTF8に変換
unicode to utf8 VOICEVOX:\u56db\u56fd\u3081\u305f\u3093\uff08\u3042\u307e\u3042\u307e\uff09
Regex.Unescape()で変換するとエスケープ文字を消せたのですが、jsonは空白でした
un escape VOICEVOX:四国めたん(あまあま) spl[2] "speakerName":"VOICEVOX:四国めたん(あまあま)" isApiKeyValid:False speakerName: audioStatusUrl:
該当のソースコード
C#
1 [System.Serializable] 2 public class Response 3 { 4 //リクエストが成功していればtrue 5 public bool success; 6 public bool isApiKeyValid; 7 public string SpeakerName; 8 public string AudioStatusUrl; 9 public string WavDownloadUrl; 10 public string Mp3DownloadUrl; 11 public string Mp3StreamingUrl; 12 13 public static Response Deserialize(string json) 14 { 15 Response res = JsonUtility.FromJson<Response>(json); 16 return res; 17 } 18 public static string Serialize(Response res) 19 { 20 string json = JsonUtility.ToJson(res); 21 return json; 22 } 23 public static string ToString(Response res) 24 { 25 return $"success:{res.success}\n" + 26 $"isApiKeyValid:{res.isApiKeyValid}\n" + 27 $"speakerName:{res.SpeakerName}\n" + 28 $"audioStatusUrl:{res.AudioStatusUrl}\n" + 29 $"wavDownloadUrl:{res.WavDownloadUrl}\n" + 30 $"mp3DownloadUrl:{res.Mp3DownloadUrl}\n" + 31 $"mp3StreaminUrl:{res.Mp3StreamingUrl}\n"; 32 } 33 } 34 35 static readonly string urlVoicevoxWebApi = "https://api.tts.quest/v3/voicevox/synthesis"; 36 37 public void StartSpeak(int speakerID, string message) 38 { 39 string url = urlVoicevoxWebApi; 40 message = WWW.EscapeURL(message+"&"); 41 url += $"?text={message}speaker={speakerID}"; 42 StartCoroutine(SendRequestGETText(url)); 43 } 44 45 public static IEnumerator SendRequestGETText(string url) 46 { 47 using (UnityWebRequest request = UnityWebRequest.Get(url)) 48 { 49 yield return request.SendWebRequest(); 50 if (request.result == UnityWebRequest.Result.ConnectionError) 51 { 52 result = request.error; 53 yield break; 54 } 55 else if (request.result == UnityWebRequest.Result.ProtocolError) 56 { 57 result = request.error; 58 yield break; 59 } 60 else 61 { 62 result = request.downloadHandler.text; 63 Debug.Log($"Response success result\n{result}"); 64 result = decodeResult(result); 65 Debug.Log($"after decode result\n{result}"); 66 Response res = Response.Deserialize(result); 67 Debug.Log(Response.ToString(res)); 68 yield break; 69 } 70 } 71 } 72 73 public static string decodeResult(string result) 74 { 75 //項目ごとに分割 76 string[] spl = result.Split(","); 77 string s=string.Empty; 78 for(int i = 0; i < spl.Length; i++) 79 { 80 if (i > 1) 81 { 82 //キーとValueの分割位置を取得 83 int mid = spl[i].IndexOf(":"); 84 // : 以降のValueの文字長を取得 85 int after = spl[i].Length - mid; 86 //Valueを抜き出してUTF8に変換 87 string enc = UnicodeToUtf8( spl[i].Substring( mid + 2, after - 3) ); 88 Debug.Log("unicode to utf8\n"+enc); 89 90 //名前(全角文字)の場合のみ 91 if (i == 2) 92 { 93 //Valueからエスケープ文字を除去 94 enc = System.Text.RegularExpressions.Regex.Unescape(enc); 95 Debug.Log("un escape\n" + enc); 96 } 97 98 // キーと値に分割 99 string[] sp = spl[i].Split(":"); 100 //変換したValueとキーを再結合 101 spl[i] = $"{sp[0]}:\"{enc}\""; 102 } 103 s += spl[i]+","; 104 } 105 // 末尾の ", を削除 106 s = s.Substring(0, s.Length - 2); 107 //Debug.Log(s); 108 109 return s; 110 } 111 112 //Unicode -> UTF8 113 public static string UnicodeToUtf8(string unicodeStr) 114 { 115 System.Text.Encoding uni = System.Text.Encoding.Unicode; 116 byte[] uniByte = uni.GetBytes(unicodeStr); 117 System.Text.Encoding utf8 = System.Text.Encoding.UTF8; 118 byte[] utf8Byte = System.Text.Encoding.Convert(uni, utf8, uniByte); 119 char[] utfChars = new char[utf8.GetCharCount(utf8Byte, 0, utf8Byte.Length)]; 120 utf8.GetChars(utf8Byte, 0, utf8Byte.Length, utfChars, 0); 121 return new string(utfChars); 122 } 123
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
URL部分
,"audioStatusUrl":"https:\/\/audio1.tts.quest\/v1\/data\/ebf199a7038db805a9840e6a2cd5b630c6849d95fd32ff8c99d8a0940ae6dec6\/status.json" unicode to utf8 https:\/\/audio1.tts.quest\/v1\/data\/ebf199a7038db805a9840e6a2cd5b630c6849d95fd32ff8c99d8a0940ae6dec6\/status.json un escape https://audio1.tts.quest/v1/data/ebf199a7038db805a9840e6a2cd5b630c6849d95fd32ff8c99d8a0940ae6dec6/status.json
WWW.EscapeURL()も試してみたのですが
Jsonにデシリアライズするときにエラーになってダメでした。
ArgumentException: JSON parse error: Missing a closing quotation mark in string. UnityEngine.JsonUtility.FromJson
補足
特になし
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/02/21 01:11