Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by brianchan661 · Sep 08, 2015 at 12:35 PM · encoding

reading json file with chinese character using streamReader

I have a json text file with chinese character.
the character is shown perfectly in notepad++ when I chosse the encoding as UTF8.
Hence I set the streamReader's encoding as UTF8 too.
But the character is not read correctly and log as some unknown character.

 public class SpeechManager :MonoBehaviour {
     
     public string fileName;
     public string speechJson;
     public int startId;
     public int endId;
     
     public Dictionary<int, Speech> speechDictionary=new Dictionary<int, Speech>(); 
     
     
     void Start(){
         getSpeechData ();
         putAllSpeechToDictionary ();
         getSpeechByID (100001);
         getSpeechByID (100002);
         getSpeechByID (100003);
 
     }
     
     
     
     public void getSpeechData(){
         try
         {
             string fileFullPath= Application.dataPath+"/textFiles/"+fileName;
             StreamReader theReader = new StreamReader(fileFullPath, Encoding.UTF8);
             speechJson=theReader.ReadToEnd();
             theReader.Close();
             
         }
         catch (Exception e)
         {
             Debug.Log(e);
         }
     }
     
     public void putAllSpeechToDictionary(){
         var N = JSON.Parse (speechJson);
         Debug.Log ("試試 "+N.Count);   // the chinese character print perfectly here
         for(int i=0;i<N.Count;i++){
             Debug.Log (N[i]);    // but failed here
             int id = N[i]["ChatID"].AsInt;
             int headiconID= N[i]["HeadiconID"].AsInt;
             int chatRole= N[i]["ChatRole"].AsInt;
             int chatstring= N[i]["ChatString"].AsInt;
             int triggerType= N[i]["TriggerType"].AsInt;
             int triggerScene= N[i]["TriggerScene"].AsInt;
             int triggerPic= N[i]["TriggerPic"].AsInt;
             int nextChatID= N[i]["NextChat"].AsInt;
             string speechCharacter= N[i]["SpeechCharacter"];
             string speechString= N[i]["SpeechString"];
             Speech tempSpeech= new Speech(id, headiconID,chatRole,chatstring,triggerType,triggerScene,triggerPic,nextChatID,speechCharacter,speechString);
             Debug.Log ("Adding speech "+ id);
             speechDictionary.Add(id, tempSpeech);
         }
     }
     
     public void getSpeechByID(int speechID){
         Debug.Log ("getting speech"+ speechID);
         Speech speechReturn= speechDictionary[speechID];
         Debug.Log (speechReturn.ToString ());
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Sep 08, 2015 at 12:50 PM 0
Share

If you debug.log speechJson does the character show correctly? What are you using to parse the JSON? Perhaps it isn't handling Unicode characters correctly?

avatar image brianchan661 · Sep 08, 2015 at 01:25 PM 0
Share

no, the speechJson is also print the character incorrectly. I have tried to set the encode as unicode in the streamReader. but result in exception
System.ArgumentException: invalid utf-16 sequence at 2282288
And I am using the SimpleJson to parse the string

avatar image Dave-Carlile · Sep 08, 2015 at 05:58 PM 0
Share

When creating your stream, try...

 new StreamReader(fileFullPath, Encoding.Default, true);

Passing true at the end will let the stream reader try to detect the encoding using "byte order marks" that are written to the beginning of the file (notepad writes these when saving as UTF-8). And using Encoding.Default as the second parameter will use the system's default encoding if it can't detect from the file.

Anyway, kind of a guess, but give it a go.

Out of curiosity, are there other Chinese or unicode characters that work? Or do they all fail?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with special char android!! How can i fix my problem? 1 Answer

How to UnEscape "& quot ;" in a string? 0 Answers

Lightmap encoding high quality 2 Answers

Split heavy operation using coroutine 0 Answers

calling Encoding.UTF8.GetBytes(JSON) in a dll causes exception, while in src format everything works great. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges