如何验证JSON对象? [英] How to validate JSON object?

查看:89
本文介绍了如何验证JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在服务器端验证传入的json对象的正确性.有没有标准/最佳的方法来做到这一点?您的验证方式是什么?

I would like to validate incoming json object for correctness at the server side. Is there a standard / optimal way to do that? What is your approach of validation?

推荐答案

我的建议-反序列化JSON,看看它是否损坏.例如,如果您在服务器端使用C#,则可以使用新的DataContractJsonSerializer,也可以使用JavaScriptSerializer进行旧的操作,这可能会简单得多.

My advice - deserialize the JSON and see if it breaks. For example, if you're using C# on the server side, you can use the newfangled DataContractJsonSerializer, or do it the old way with the JavaScriptSerializer which is arguably much simpler.

var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<Dictionary<string, object>>(jsonString);

现在,事实证明您正在使用Java,当然我的C#示例对您不起作用,但是概念是相同的. Stackoverflow已在此处中提供了一些答案:

And now that it's come out that you're using Java, of course my C# example is not going to work for you, but the concept is the same. Stackoverflow already has some answers here: Convert a JSON string to object in Java ME?

这篇关于如何验证JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆