Tag: json

如何使用PHP cURL发布JSON数据?

这是我的代码, $url = 'url_to_post'; $data = array("first_name" => "First name","last_name" => "last name","email"=>"email@gmail.com","addresses" => array ("address1" => "some address" ,"city" => "city","country" => "CA", "first_name" => "Mother","last_name" => "Lastnameson","phone" => "555-1212", "province" => "ON", "zip" => "123 ABC" ) ); $data_string = json_encode($data); $ch=curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string)); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, […]

JSONP请求error handling

我正在做ajax jsonp请求,但失败error handling将无法正常工作。 如果请求是404或500,则不会处理该错误。 我一直在四处寻找答案,但找不到任何东西。 似乎有一个与http://code.google.com/p/jquery-jsonp/解决scheme,但我找不到如何使用它的任何示例。 function authenticate(user, pass) { $.ajax ({ type: "POST", url: "url", dataType: 'jsonp', async: false, //json object to sent to the authentication url data: {"u": userid, "p": pass}, success: function (data) { //successful authentication here console.log(data); }, error: function(XHR, textStatus, errorThrown) { alert("error: " + textStatus); alert("error: " + errorThrown); […]

使用常规编码器使对象JSON可序列化

JSON序列化自定义不可序列化对象的常规方法是json.JSONEncoder ,然后将自定义编码器传递给转储。 它通常是这样的: class CustomEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, foo): return obj.to_json() return json.JSONEncoder.default(self, obj) print json.dumps(obj, cls = CustomEncoder) 我想要做的是使用默认的编码器可序列化的东西。 我环顾四周,却找不到任何东西。 我的想法是,编码器会查看一些字段来确定json编码。 类似于__str__ 。 也许是一个__json__字段。 在Python中有这样的东西吗? 我想使我所做的一个模块类成为JSON可序列化给使用该包的每个人,而不用担心自己实现自己的[自定义]自定义编码器。

JavaScript通过json数组循环?

我想循环通过以下的JSON数组: { "id": "1", "msg": "hi", "tid": "2013-05-05 23:35", "fromWho": "hello1@email.se" }, { "id": "2", "msg": "there", "tid": "2013-05-05 23:45", "fromWho": "hello2@email.se" } 并尝试了以下 for (var key in data) { if (data.hasOwnProperty(key)) { console.log(data[key].id); } } 但由于某种原因,我只得到第一部分,身份证1值。 有任何想法吗?

将HTML映射到JSON

我试图将HTML映射到结构完整的JSON。 有没有图书馆这样做,或者我需要写我自己的? 我想如果没有html2json库,那么我可以把一个xml2json库作为开始。 毕竟,html只是xml的一个变体呢? 更新:好的,我应该举一个例子。 我想要做的是以下。 parsing一个html的string: <div> <span>text</span>Text2 </div> 到一个像这样的json对象: { "type" : "div", "content" : [ { "type" : "span", "content" : [ "Text2" ] }, "Text2" ] } 注意 :如果您没有注意到标签,我正在寻找一个Javascript的解决scheme

如何检查给定的string在Java中是否是有效的JSON

如何在Java中validationJSONstring? 或者我可以使用正则expression式parsing它?

Web API Put请求生成一个Http 405方法不允许的错误

这里是对我的Web API的PUT方法的调用 – 方法的第三行(我从ASP.NET MVC前端调用Web API): client.BaseAddress是http://localhost/CallCOPAPI/ 。 这里是contactUri : 这里是contactUri.PathAndQuery : 最后,这是我的405回应: 这是Web API项目中的WebApi.config: public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Routes.MapHttpRoute( name: "DefaultApiGet", routeTemplate: "api/{controller}/{action}/{regionId}", defaults: new { action = "Get" }, constraints: new { httpMethod = new HttpMethodConstraint("GET") }); var […]

json.dumps vs flask.jsonify

我不确定我了解flask.jsonify方法的用途。 我尝试从这个jsonstring: data = {"id": str(album.id), "title": album.title} 但是我得到的json.dumps和json.dumps不同。 json.dumps(data): [{"id": "4ea856fd6506ae0db42702dd", "title": "Business"}] flask.jsonify(data): {"id":…, "title":…} 显然我需要得到更像是什么json.dumps返回的结果。 我究竟做错了什么?

通过JSON发布一个对象数组到ASP.Net MVC3

我正在寻找一种解决scheme,通过JSON将对象数组发布到MVC3。 我正在使用的示例代码: http : //weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx JS: var data = { ItemList: [ {Str: 'hi', Enabled: true} ], X: 1, Y: 2 }; $.ajax({ url: '/list/save', data: JSON.stringify(data), success: success, error: error, type: 'POST', contentType: 'application/json, charset=utf-8', dataType: 'json' }); ListViewModel.cs: public class ListViewModel { public List<ItemViewModel> ItemList { get; set; } public float X { […]

将JSONstringparsing为JavaScript中的特定对象原型

我知道如何parsing一个JSONstring,并把它变成一个JavaScript对象。 您可以在现代浏览器(和IE9 +)中使用JSON.parse() )。 这很好,但是我怎样才能把这个JavaScript对象变成一个特定的 JavaScript对象(即用一个特定的原型)呢? 例如,假设你有: function Foo() { this.a = 3; this.b = 2; this.test = function() {return this.a*this.b;}; } var fooObj = new Foo(); alert(fooObj.test() ); //Prints 6 var fooJSON = JSON.parse({"a":4, "b": 3}); //Something to convert fooJSON into a Foo Object //……. (this is what I am missing) alert(fooJSON.test() ); //Prints […]