首页 \ 问答 \ HTML编码的字符串可以转换为JSON吗?(Can an HTML encoded string be converted to JSON?)

HTML编码的字符串可以转换为JSON吗?(Can an HTML encoded string be converted to JSON?)

我从服务器获取编码数据,使用.NET WebUtility.HtmlEncode编码。

然后显示该数据,并且需要将其发送回服务器以进行某些操作。 在此期间,它在使用JSON.stringify发送之前转换为JSON。 到目前为止一切正常。

但是,一旦到达服务器,它就会因为潜在的危险而被拒绝。 转换为JSON的对象可以包含具有特殊字符的字符串,例如 -

“这是John&#39s帐户”最初“这是John的帐户”

或者“约翰收入&#165”最初“约翰以¥收入”

我相信这些编码的字符串值会干扰正确形成的JSON。

在Javascript中我有什么方法可以JSONify HTML编码的字符串?

编辑:如果不清楚,当我执行JSON.stringify(数据)时,数据已经编码。 我的数据的一个例子 -

row [0] = {column1,column2,column3}其中每列是HTML编码的字符串,例如“This is John&#39s account”


I'm getting encoded data from the server, which is encoded using .NETs WebUtility.HtmlEncode.

This data is then displayed and needs to be sent back to the server for some operations. During this time, it is converted to JSON before being sent over using JSON.stringify. All works fine so far.

However, once this reaches the server, it is rejected due to being potentially dangerous. The object that is converted to JSON can have strings with special chars such as -

"This is John&#39s account" originally "This is John's account"

Or "John earns in &#165" originally "John earns in ¥"

My belief is that these encoded string values are interfering with the JSON being properly formed.

Is there any way in Javascript that I can JSONify HTML encoded strings?

EDIT: In case it's not clear, the data is already encoded when i do JSON.stringify(data). An example of my data -

row[0] = {column1, column2, column3} Where each column is an HTML encoded string such as "This is John&#39s account"


原文:https://stackoverflow.com/questions/16323358
更新时间:2023-10-14 20:10

最满意答案

你必须提供你自己的比较函数,当实例化地图时,它必须作为第三个模板参数传递。 例如:

struct Comp
{
  bool operator()(const std::string& lhs, const std::string& rhs) const
  {
    // implement your comparison logic here
  }
};

这个类的实例是可调用的(因此“functor”)带有两个字符串参数,并且应该基于严格的弱排序逻辑返回true或false。

然后使用函子类型实例化地图:

std::map<string, list<pair<string, int>>, Comp> List;

现在,地图将在内部使用您的比较逻辑来定义其元素的排序。


You have to provide your own comparison functor, which must be passed as 3rd template parameter when instantiating the map. For example:

struct Comp
{
  bool operator()(const std::string& lhs, const std::string& rhs) const
  {
    // implement your comparison logic here
  }
};

Instances of this class is callable (hence "functor") with two string parameters, and should return true or false based in a strict weak ordering logic.

Then instantiate the map using the functor type:

std::map<string, list<pair<string, int>>, Comp> List;

Now the map will use your comparison logic internally to define the ordering of its elements.

相关问答

更多
  • 在C ++ 17中有一个新特性,它允许更改键,它的功能是extract()例子: std::map m{ {10, "potato"}, {1, "banana"} }; auto nodeHandler = m.extract(10); nodeHandler.key() = 2; m.insert(std::move(nodeHandler)); // { { 1, "banana" }, { 2, "potato" } } In C++17, the new ma ...
  • 是的你可以。 std::shared_ptr有一个operator<用适合map key使用的方式定义。 具体而言,只比较指针值,而不是引用计数。 Yes you can. std::shared_ptr has operator< defined in a way appropriate for map key usage. Specifically, only pointer values are compared, not reference counts. Obviously, the pointe ...
  • 你必须提供你自己的比较函数,当实例化地图时,它必须作为第三个模板参数传递。 例如: struct Comp { bool operator()(const std::string& lhs, const std::string& rhs) const { // implement your comparison logic here } }; 这个类的实例是可调用的(因此“functor”)带有两个字符串参数,并且应该基于严格的弱排序逻辑返回true或false。 然后使用函子类型实例 ...
  • 您将不得不创建另一个地图,并将第一个地图的元素复制到第二个地图中,如下所示: std::map map1; ... // operations on map1 std::map map2(map1.begin(), map1.end()); You would have to create another map, and copy the elements of the first map into the second, like so: std::map ...
  • gamedev.net有几个选项 。 仔细查看Fruny发布的帖子。 旁白:为什么不将Boost视为可能的解决方案提供商? 对于专业的C ++编码人员来说,这是一个受人尊敬,同行评估,记录良好的解决方案。 There are several options gamedev.net. Look down the thread for the posting by Fruny. Aside: Why would you not consider Boost as a possible solution prov ...
  • 我保证按照int顺序排序吗? 是。 默认的比较器是std::less ,在你的情况下它是std::less ,它只是按照预期的那样使用< 。 是否有必要定义一个比较类来强制排序? 不,因为之前的答案是“是”! 分拣何时实际发生? 典型的map实现使用比较器将新元素插入到正确的位置。 查找时也使用比较器。 Is m guaranteed to be sorted according to int order? Yes. The default comparator is std::less ...
  • std::map将比较对象作为模板参数,因此要执行您想要的类型,您可以在运行时更改行为。 struct MoreOrLess { bool useLess; template bool operator()(const T &t, const U &u) const { if(useLess) return t < u; else return t > u; } ...
  • 您的操作opeator<不会正确排序您的元素,因为如果Typ较小或者Length较小,则返回true。 #include bool operator < (const OccTestdef& R) const { return std::tie(Typ, Length) < std::tie(R.Typ, R.Length); } Your opeator< will not order your elements properly because you return true ...
  • 设置map2 = map1实际上将复制整个对象,而不仅仅是元素。 你可能想做的是 map2.clear(); map2.insert(map1.begin(), map1.end()); 我完全相信两个步骤的复杂性将是O(n log n) ,但不要引用我的话。 编辑 更好( O(n) ): map2.clear(); map2.insert(map1.rbegin(), map1.rend()); “对于第三个版本(插入(第一个,最后一个)),一般是Nlog(大小+ N)(其中N是第一个和最后一个之间的 ...
  • 不,这不安全,因为当QObject被销毁时, QPointer可能会变为NULL 。 ( QPointer类似于std::weak_ptr 。)所以这段代码会产生Undefined Behavior: class CopyableWidget : public QWidget { Q_OBJECT; public: CopyableWidget(Widget* parent = 0) : QWidget(parent) {} CopyableWidget(const CopyableWid ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。