繁体   English   中英

将json对象序列化或传递到Ajax

[英]Serializing or passing json objects into Ajax

返回了Json对象,我想在JavaScript中提取数据就如何传递循环字段并提取数据而言

def For_Sale_Listing(request,id):
    try:
        listing = Listing.objects.filter(pk=id)
        listing_json = serializers.serialize("json", listing)
    except Listing.DoesNotExist:
        raise Http404('listing not found!')
    data = {"listing_json": listing_json}
    return JsonResponse(data)
$.ajax({
    url: "/For-Sale-Listing"+"/" + parseInt(data[0]),
    type: "GET",
    data: {'Subscription': 1},
          success: function(data) {
           console.log(JSON.parse(data.listing_json));
          },
          error: function(xhr, status, error) {
          }
        });

我想使用javascript传递字段

[{"model": "listings.listing", "pk": 3, "fields": {"property_type": 1, "user": 1, "price": 13000000, "roomsTotal": 78, "Bathrooms": 6, "bedrooms": 67, "Receptionrooms": 67, "livingArea": "67.000", "lotSize": "67.000", "unitType": 1, "yearBuilt": null, "remodelYear": null, "hoaPrice": null, "groundTankSize": null, "garageSize": null, "homeDescription": "", "whatYouLoveDescription": "", "Dishwasher": false, "Dryer": false, "Freezer": false, "GarbageDisposal": false, "Microwave": false, "Oven": false, "Refrigerator": false, "Washer": false, "RadioGroup_Rashan": null, "ACarpet": false, "AConcrete": false, "ATiles": false, "ALinoleum": false, "ADSoftwood": false, "ADOther": false, "BreakfastNook": false, "DiningRoom": false, "FamilyRoom": false, "LaundryRoom": false, "Library": false, "MasterBath": false, "Office": false, "Workshop": false, "roomCount": null, "attic": false, "cableReady": false, "ceilingFan": false, "doublePaneWindows": false, "fireplace": false, "intercom": false, "jettedTub": false, "securitySystem": false, "CCentral": false, "CEvaporative": false, "CGeothermal": false, "CRefrigeration": false, "CSolar": false, "CWall": false, "COther": false, "CNone": false, "HForcedAir": false, "HGeothermal": false, "HHeatPump": false, "HRadiant": false, "HStove": false, "HWall": false, "HOther": false, "FCoal": false, "FElectric": false, "FGas": false, "FOil": false, "FPropaneButane": false, "FSolar": false, "FWoodPellet": false, "FOther": false, "FNone": false, "basketballCourt": false, "doorman": false, "elevator": false, "fitnessCenter": false, "gatedEntry": false, "nearTransportation": false, "tennisCourt": false, "RadioGroup_Architectural": null, "Brick": false, "CementConcrete": false, "Stone": false, "EOther": false, "FloorCount": null, ...

我不确定您要问的是什么,但如果您想读取结果,可以100%地使用JSON对象中的属性名称。 像这样:

var data = {"model": "listings.listing", "pk": 3, "fields": {"property_type": 1, "user": 1, "price": 13000000, "roomsTotal": 78, "Bathrooms": 6}};

var test = "Model: " + data.model + "<br/>PK:" + data.pk + "<br/> Fields: <br/> Property_Type:" + data.fields.property_type;

------Result----
Model: listings.listing
PK:3
Fields:
Property_Type:1

提琴手

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM