1

How to access the chinese json data inside the Js render template.

Anyone please update me on this?

<script type="text/x-jsrender">
    <div>
       <em>Name:</em> {{:代理商}}
      </div>
     </script>


[data:
    [
      {
        "代理商": "Robert",

      }
    ]

2 Answers2

1

Unfortunately this is an issue that arises from JsRender's use of RegEx expressions to parse data.

In JavaScript, until browsers support ES6, there is only very limited RegEx Unicode support, and in particular \w in JavaScript Regex expressions is ASCII-based.

So for now, using arbitrary Unicode character sequences as keys on JavaScript objects passed to JsRender templates is not supported.

See Javascript + Unicode regexes and http://speakingjs.com/es5/ch24.html for more information...

Community
  • 1
  • 1
BorisMoore
  • 8,444
  • 1
  • 16
  • 27
0

I've attempted inserting your data into the demo section of https://www.jsviews.com/

This appears related to your use of unicode characters for keys. Javascript is completely fine with using unicode keys for objects, so it is likely something inside the JsRender library itself: How can i use Unicode string key In Javascript Object?

JsRender appears to work appropriately if you switch the order of your key/value pairs (I understand this is not a solution to your problem, but shows that the keys are the issue):

<script type="text/x-jsrender">
  <div>
   <em>Name:</em> {{:Robert}}
  </div>
 </script>

data:
[
  {
    "Robert": "代理商",

  }
]

May I ask how you are obtaining your data/templates? Rather, would it be possible to update your keys to a non-unicode value?

Community
  • 1
  • 1
Hodrobond
  • 1,665
  • 17
  • 18