2014-06-10 42 views
0

繼檢索如何存儲值是JSON的一部分從JSON

savedQuestionOptionsList: [ 
{ 
id: 3, 
oid: { 
id: 1, 
name: "Excellent", 
deleted: false, 
version: 0 
}, 
cqid: { 
id: 19, 
pqid: { 
id: 9, 
qid: { 
id: 2, 
category: "default", 
question: "Staff Response", 
cid: null, 
deleted: false, 
version: 0 
}, 
pid: { 
id: 2, 
category: "default", 
name: "Food And Beverage", 
cid: null, 
deleted: false, 
version: 0 
}, 
deleted: false, 
version: 0 
}, 
qtid: { 
id: 4, 
name: "option", 
deleted: false, 
version: 0 
}, 
cid: { 
id: 1, 
name: "Taj", 
createdDate: { 
minimalDaysInFirstWeek: 1, 
time: { 
time: 1402030754000, 
minutes: 29, 
seconds: 14, 
hours: 10, 
month: 5, 
timezoneOffset: -330, 
year: 114, 
day: 5, 
date: 6 
}, 
weekYear: 2014, 
weeksInWeekYear: 52, 
gregorianChange: { 
time: -12219292800000, 
minutes: 30, 
seconds: 0, 
hours: 5, 
month: 9, 
timezoneOffset: -330, 
year: -318, 
day: 5, 
date: 15 
}, 
timeZone: { 
lastRuleInstance: null, 
DSTSavings: 0, 
rawOffset: 19800000, 
ID: "Asia/Calcutta", 
dirty: false, 
displayName: "India Standard Time" 
}, 
lenient: true, 
timeInMillis: 1402030754000, 
firstDayOfWeek: 1, 
weekDateSupported: true 
}, 
deleted: false, 
version: 0 
}, 
deleted: false, 
version: 0 
}, 
deleted: false, 
version: 0 
}, 
{ 
id: 4, 
oid: { 
id: 4, 
name: "Poor", 
deleted: false, 
version: 0 
}, 
cqid: { 
id: 20, 
pqid: { 
id: 9, 
qid: { 
id: 2, 
category: "default", 
question: "Staff Response", 
cid: null, 
deleted: false, 
version: 0 
}, 
pid: { 
id: 2, 
category: "default", 
name: "Food And Beverage", 
cid: null, 
deleted: false, 
version: 0 
}, 
deleted: false, 
version: 0 
}, 
qtid: { 
id: 4, 
name: "option", 
deleted: false, 
version: 0 
}, 
cid: { 
id: 1, 
name: "Taj", 
createdDate: { 
minimalDaysInFirstWeek: 1, 
time: { 
time: 1402030754000, 
minutes: 29, 
seconds: 14, 
hours: 10, 
month: 5, 
timezoneOffset: -330, 
year: 114, 
day: 5, 
date: 6 
}, 
weekYear: 2014, 
weeksInWeekYear: 52, 
gregorianChange: { 
time: -12219292800000, 
minutes: 30, 
seconds: 0, 
hours: 5, 
month: 9, 
timezoneOffset: -330, 
year: -318, 
day: 5, 
date: 15 
}, 
timeZone: { 
lastRuleInstance: null, 
DSTSavings: 0, 
rawOffset: 19800000, 
ID: "Asia/Calcutta", 
dirty: false, 
displayName: "India Standard Time" 
}, 
lenient: true, 
timeInMillis: 1402030754000, 
firstDayOfWeek: 1, 
weekDateSupported: true 
}, 
deleted: false, 
version: 0 
}, 
deleted: false, 
version: 0 
}, 
deleted: false, 
version: 0 
} 
], 

要取回我使用下面的jQuery的方式

$.each(data1.savedQuestionOptionsList, function(index, currQueOpt) { 
        console.log(currQueOpt.oid.id +"cqid id "+currQueOpt.cqid.id); 

我有一個表TR

<tr> 
    <td><input id="checked2" type="checkbox" class="cd" value="2" style="margin:6px 0 0 0;"></td> 
    <td style="padding:6px 0 0 0;"><b>Staff Response</b></td> 
    <td> 
     <select name="questionType" id="questionType" class="qType QSelect" style="padding:3px;"> 
     <option value="">--Select--</option> 
     <option value="1">text</option> 
     <option value="2">rating</option> 
     <option value="3">boolean</option> 
     <option class="show-checkboxes" value="4">option</option> 
     </select> 
    </td> 
    <td width="35%" style="padding:10px 0 0 0;" class="Fsize12 out" id="a2"><a href="#" class="checkbox-inline" onclick="getModal(this)">Get Options</a></td> 
    <td><input type="hidden" id="optionInputa2"></td> 
</tr> 

現在我要求追加oid.name的值(在給定的JSON oid.name提供優異的和差2個值和兩個具有p個qid.qid.id的值爲2)至以上的td,其類別爲out。

我可以這樣做

$.each(data1.savedQuestionOptionsList, function(index, currQueOpt) { 
        console.log(currQueOpt.oid.id +"cqid id "+currQueOpt.cqid.id); 

        $('#checked'+currQueOpt.cqid.pqid.qid.id).closest('tr').find('.out').html  (currQueOpt.oid.name); 

但是我有追加,所以我不能每個function.Is有內部直接使用任何方式來存儲qid.id和oid.name之前和之後做一些處理我可以循環和追加

+0

完整JSON可以發現[這裏](http://pastie.org/9276433) – SpringLearner

+0

代碼的第一片可以是JS,但它不是JSON(如密鑰必須之間'「' )該代碼是否已經存儲在調試器中? –

回答

0

例如,您可以創建另一個HTML塊,其屬性爲:style="display: none",並用ID標記所有內部元素,在其中附加解析的JSON值,然後使用jQuery選擇並處理它。

而且,裏面$.each聲明,你可以調用一些回調函數,會先proccess數據,然後將其追加它需要的地方。

+0

感謝您的回答,Unfort不管怎樣,我不能按照你的方式行事。 – SpringLearner

0

不知道如果我明白你需要什麼,但這裏有我的兩分錢:我認爲最簡單的方法就是創建一個附配的變量和存儲在那裏你想要什麼:

var aux=[]; 

$.each(data1.savedQuestionOptionsList, function(index, currQueOpt) { 
     console.log(currQueOpt.oid.id +"cqid id "+currQueOpt.cqid.id); 
     aux.push({name: currQueOpt.oid.name, id:currQueOpt.cqid.pqid.qid.id}); 
    } 
); 

然後你就可以處理這些值:

for (var i=0;i< aux.length;i++) { 
    //process aux[i].id 
    //process aux[i].name 
} 
+0

謝謝answering.pqid.qid.id不過該行id.So我必須把currQueOpt.oid.name的值的行(pqid.qid.id)在上面的JSON QID在重複 – SpringLearner

+0

我只是以這些值爲例,你可以存儲你需要的輔助數據。 –

+0

你可以來聊天室,讓我們可以更詳細地http://chat.stackoverflow.com/rooms/44929/java-spring-jsf-struts-hibernate-php-jquery-javascript-beginners這個討論 – SpringLearner