我在通过jQuery循环JSON结构时遇到了一些麻烦,
这是我的JSON数据:
{
"suppliers": [
{
"Supplier": {
"id": "5704ebeb-e5e0-4779-aef4-16210a00020f",
"name": "Gillmans",
"mobile": "",
"office_telephone": "00000",
"ooh_contact": "00000",
"fax_number": "",
"address_line_1": "St Oswalds Road",
"address_line_2": "Gloucester",
"address_line_3": "",
"address_line_4": "",
"postcode": "GL1 2SG",
"email": "email@example.com",
"contact": "",
"position": "",
"aov": "180.00",
"engineer": false,
"cc_on_new_job_emails": true,
"can_add_quotes": false,
"notes": "",
"status": "1",
"created": "2016-04-06 11:58:51",
"modified": "2016-07-27 11:23:01",
"status_text": "Active",
"engineer_text": "No",
"cc_on_new_job_emails_text": "Yes"
},
"Trade": [],
"PostcodeArea": []
},
{
"Supplier": {
"id": "571e390f-91e8-4745-8f78-168b0a00020f",
"name": "Kings",
"mobile": "",
"office_telephone": "00000", …Run Code Online (Sandbox Code Playgroud) 在我的javascript代码中,我从cs文件中获取json字符串
var tmpString="<%=resultset2%>";
Run Code Online (Sandbox Code Playgroud)
在cs我连接字符串来构建json字符串.这是一个问题,json字符串作为字符串返回,它有"与它.
"[{id:'1',name:'Aik'},{id:'2',name:'Aik or Aik'}]"
Run Code Online (Sandbox Code Playgroud)
因为"在开头和结尾javascript代码将其视为一个字符串.请指导我如何解决这个问题.
谢谢
我在[Exception: SyntaxError: Unexpected token :]尝试评估以下表达式时遇到错误:
eval("{"T1": [1,2,3,4,5,6,7,8,9,10,11,12], "T2": [12,11,10,9,8,7,5,4,3,2,1]}")
Run Code Online (Sandbox Code Playgroud)
但是,没有"作品的相同表达:
eval({"T1": [1,2,3,4,5,6,7,8,9,10,11,12], "T2": [12,11,10,9,8,7,5,4,3,2,1]})
Run Code Online (Sandbox Code Playgroud)
如果我的JSON是字符串格式,就像在第一个示例中一样,我如何将其转换为javascript对象?
如果我尝试使用:
JSON.parse("{"T1": [1,2,3,4,5,6,7,8,9,10,11,12], "T2": [12,11,10,9,8,7,5,4,3,2,1]}")
Run Code Online (Sandbox Code Playgroud)
我得到[Exception: SyntaxError: Unexpected identifier].我该"怎么逃避?
note:
{"category_id":"1","name":"Notes","icon":"images\/note.png"},
quote:
{"category_id":"2","name":"Quotes","icon":"images\/quote.png"},
project:
{"category_id":"3","name":"Projects","icon":"images\/project.png"},
skill:
{"category_id":"4","name":"Skills","icon":"images\/skill.png"}
Run Code Online (Sandbox Code Playgroud)
这是我的控制台中显示的内容.我已经用于json_decode我的查询,并与字符串连接,我相信这是问题的原因.
那么如何将字符串数据转换为json格式?
我有问题...我有以下变量(类型:字符串): {"friendCount":5,"pnCount":0,"allCount":5}
我的目标是,有3个变量与friendCount,pnCount和allCount,我不知道,我怎么能这样做.这看起来像一个对象,但它只是一个字符串(计划好)
我认为我必须使用RegEx或其他东西,但我没有进一步的想法......
字符串中的数字可以在0到约100之间
我希望,你明白我的问题,可以帮助我:)
我正在调用Google表格API中的数据,每行的内容如下所示:
{
$t: "title: Test Title, gamelabel: Test Game, startdate: 2016-06-14"
}
Run Code Online (Sandbox Code Playgroud)
是否有任何现成的/简单的方法将此字符串转换为此格式的javascript对象?
{
title : 'Test Title',
gamelabel: 'Test Game',
startdate: '2016-06-14
}
Run Code Online (Sandbox Code Playgroud)
注意,键需要是动态的(为表单的标题创建键),因此无法准确知道键将在哪些键中$t.
更新:使用JSON.parse()在这里不起作用,我想通过以下方式实现此目的有一种hacky-ish方式:
var temp = {};
var params = $t.split(/:/g);
for(var i = 0; i<params.length; i += 2){
temp[params[i].trim()] = params[i + 1].trim();
}
Run Code Online (Sandbox Code Playgroud)
这实际上不适用于提供的格式/可能是一个开始,但我不知道这里的最佳做法是什么.
我的服务器根据请求,从节点mysql查询服务JSON,但只有名称行.例:
{
"Name": "Charles"
}, etc.
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得Name的值并将其放入数组中?说我有这个,
[
{
"Name": "Charles"
},
{
"Name": "Alex"
}
]
Run Code Online (Sandbox Code Playgroud)
我怎么能把查尔斯和亚历克斯变成阵列?
喜欢:
Names = ["Charles", "Alex]?
Run Code Online (Sandbox Code Playgroud) 我在表单上有一个输入,它将值存储为对象.
jQuery('#inputId').val()
Run Code Online (Sandbox Code Playgroud)
返回类似的东西
'[{"Id":"123","Name":"A","PathOfTerm":"A","Children":[],"Level":0,"RawTerm":null},{"Id":"234","Name":"B","PathOfTerm":"B","Children":[],"Level":0,"RawTerm":null}]'
Run Code Online (Sandbox Code Playgroud)
作为单个字符串.有没有办法阻止它自动转换为字符串(可能不使用.val?)或将其从字符串转换为我可以使用的东西?
我有一个像下面这样的字符串,
var x = "[{"k":"1"}]";
console.log(x[0].K);
Run Code Online (Sandbox Code Playgroud)
我把这个字符串挂起来,因为我是从服务器上得到的。它超出了我的控制。我想显示“k”值,当我穿上它时,得到
Uncaught SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)
我知道,这是因为数组内外的“双引号”。
它甚至不允许我解析它。
所以我认为
1)Replace "[ and ]" as '[ and ]' so it becomes
'[{"k":"1"}]'
2)Then I can parse and get the 'k' value.
var x = "[{"k":"1"}]";
console.log(x.replace(/"[/g," '[ ").replace(/"]"/g," ]'"));
Run Code Online (Sandbox Code Playgroud)
但是我仍然遇到相同的意外标识符错误,请任何人建议我帮助。Thnaks。
我想获取json响应的值。我写了一个如下的Ajax函数:
$.ajax({
url: '/v1/shopify-Ajax/ajax.php',
method: 'post',
data: {datalog: dataLog, variant: $('#prod').val()}
})
.success(function(response){
//window.location.href = "/v1/thank-you.php";
})
Run Code Online (Sandbox Code Playgroud)
我从服务器端脚本得到如下响应:
{"order":
{"id":303657910281,
"email":"test20@code1.com",
"closed_at":null,
"created_at":"2018-03-19T01:04:58-07:00",
"updated_at":"2018-03-19T01:04:58-07:00",
"number":811,
"note":null,
"token":"9709d7c295ac1dbbaf29c2d09a9d5a9d",
"gateway":"",
"test":false,
"total_price":"82.49",
"subtotal_price":"82.49",
"total_weight":null,
"total_tax":"0.00",
"taxes_included":false,
"currency":"USD",
"financial_status":"paid",
"confirmed":true,
"total_discounts":"0.00",
"total_line_items_price":"82.49","cart_token":null,"buyer_accepts_marketing":false,"name":"#1811","referring_site":null,"landing_site":null,"cancelled_at":null,"cancel_reason":null,"total_price_usd":"82.49","checkout_token":null,"reference":null,"user_id":null,"location_id":null,"source_identifier":null,"source_url":null,"processed_at":"2018-03-19T01:04:58-07:00","device_id":null,"phone":null,"customer_locale":null,"app_id":2306584,"browser_ip":null,"landing_site_ref":null,"order_number":1811,"discount_codes":[],"note_attributes":[],"payment_gateway_names":[""],"processing_method":"","checkout_id":null,"source_name":"2306584","fulfillment_status":null,"tax_lines":[],"tags":"","contact_email":"test20@code1.com","order_status_url":"https:\/\/checkout.shopify.com\/19258983\/orders\/9709d7c295ac1dbbaf29c2d09a9d5a9d\/authenticate?key=0XXX","line_items":[{"id":610330640393,"variant_id":2323256639497,"title":"XX","quantity":1,"price":"82.49","sku":"","variant_title":"3 XX","vendor":"X1","fulfillment_service":"manual","product_id":235965415433,"requires_shipping":false,"taxable":false,"gift_card":false,"pre_tax_price":"82.49","name":"XXXX","variant_inventory_management":null,"properties":[],"product_exists":true,"fulfillable_quantity":1,"grams":0,"total_discount":"0.00","fulfillment_status":null,"tax_lines":[]}],"shipping_lines":[],"billing_address":{"first_name":"","address1":"","phone":null,"city":"","zip":"","province":"","country":null,"last_name":"","address2":null,"company":null,"latitude":null,"longitude":null,"name":"","country_code":null,"province_code":null},"shipping_address":{"first_name":"test","address1":"6116 Beverly Dr","phone":null,"city":"Adair","zip":"50002","province":"Iowa","country":"United States","last_name":"tes","address2":null,"company":null,"latitude":null,"longitude":null,"name":"test tes","country_code":"US","province_code":"IA"},"fulfillments":[],"refunds":[],"customer":{"id":324158947337,"email":"test20@code1.com","accepts_marketing":false,"created_at":"2018-03-19T01:04:58-07:00","updated_at":"2018-03-19T01:04:58-07:00","first_name":"test","last_name":"tes","orders_count":1,"state":"disabled","total_spent":"0.00","last_order_id":303657910281,"note":null,"verified_email":true,"multipass_identifier":null,"tax_exempt":false,"phone":null,"tags":"","last_order_name":"#1811","default_address":{"id":371809157129,"customer_id":324158947337,"first_name":"test","last_name":"tes","company":null,"address1":"6116 Beverly Dr","address2":null,"city":"Adair","province":"Iowa","country":"United States","zip":"50002","phone":null,"name":"test tes","province_code":"IA","country_code":"US","country_name":"United States","default":true}}}}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何直接访问订单ID(无循环)并检查订单ID是否为null或空白。
!