Я в настоящее время создания ArrayList в Java, а затем запустить функцию .toJson от Google-gson на нем:Как получить доступ к элементам массива JSON в javascript?
public String statusesToJson(ArrayList<String> statuses){
Gson gson = new Gson();
return gson.toJson(statuses);
}
Какие результаты в формате JSON:
[ "u", "u", "u", "u" ]
Тогда в JSP I» м передавая его в JavaScript:
<script language="JavaScript" type="text/javascript">CheckStatus.loaded('<%=model.getPageId() %>', '<%=request.getContextPath() %>', '<%=model.arrayListToJson(model.getStatuses()) %>');</script>
Тогда в JavaScript Я разборе его в массив JSON:
CheckStatus.statuses = JSON.parse(statuses);
alert(CheckStatus.statuses);
Это то приводит следующий вывод:
u, u, u, u
Проблема заключается в том, что следующее не работает и вызывает мою страницу не нагрузка:
alert(CheckStatus.statuses[0]);
Что случилось с этим?
EDIT: Loaded Функция:
loaded : function(guid, context, statuses) {
CheckStatus.guid = guid;
CheckStatus.context = context;
CheckStatus.statuses = JSON.parse(statuses);
alert(CheckStatus.statuses[0]);
if(CheckStatus.init == null){
submitForm('checkStatusForm', CheckStatus.guid);
CheckStatus.init = true;
}
setupForm('checkStatusForm', function(){CheckStatus.validStatus();});
//CheckStatus.setImages();
applyCSS3('Check_Status');
}
Действительно Функция Статус:
validStatus : function(){
CheckStatus.params = $('#checkStatusForm').serializeObject();
if(document.getElementById('regionID').value != "" && document.getElementById('regionAction').value != ""){
submitForm('checkStatusForm', CheckStatus.guid);
}else{
error("Cannot Commit", "You must select an action before attempting to commit.");
}
},
Setup Форма Функция:
/**
* Sets up the form to submit when the user presses enter inside an input
* element. Also calls the callback when the form is submitted, does not
* actually submit the form.
*
* @param id The id of the form.
* @param callback The callback to call.
* @return Nothing.
*/
function setupForm(id, callback) {
$('#' + id + ' input').keydown(function(e) {
if (e.keyCode === 13) {
$(this).parents('form').submit();
e.preventDefault();
}
});
$('#' + id).submit(function(e) {
e.preventDefault();
callback();
});
}
Отправить форму Функция:
/**
* Serializes and submits a form.
*
* @param id
* The id of the form to submit.
* @param guid
* The guid of the page the form is on to pass to the server.
* @return nothing.
*/
function submitForm(id, guid) {
var subTabId = $('#' + id).closest('#tabs > div > div').attr(
'id'), tabId = $('#' + id).closest('#tabs > div')
.attr('id'), data = $('#' + id).serializeArray();
data.push({
name : "framework-guid",
value : guid
});
$.ajax({
type : 'POST',
cache : 'false',
url : '/pasdash-web/' + tr("_", "", tabId.toLowerCase()) + '/' + tr("_", "", subTabId)
+ '.jsp',
data : data,
success : function(html) {
$('#' + subTabId).html(html);
resourceChanged(tabId, subTabId,
$('#' + id + ' input[name="framework_command"]')[0].value,
guid);
},
error : function(jqXHR, textStatus, errorThrown) {
error('Ajax Error', textStatus);
}
});
return false;
}
«Я разбор его в массив JSON» -> Нет, это не так, вы» re parsing it _from_ массив JSON в JavaScript 'Array'. –
Что именно делает эта «загруженная» функция? Вероятно, нет никакой причины, чтобы вы могли выразить JSON как строку; это избыточно. – Pointy
Хммм .... не JSON должен быть именем: пары значений? Это похоже на обычный старый массив JS. –