添加元素JSON

时间:2016-07-14 21:05:24

标签: javascript jquery html json

我想将新元素添加到JSON ...

Dim x, y, z
On Error Goto ErrorHandler
x = 30
y = 0
z = x / y 'would like to grab this code since this is where the error occurred
Exit 


ErrorHandler:
debug.Log(Err.Description + _
" error occurred on line number " + _
CText(Err.LineNumber))

这是添加元素的功能,但不起作用......

public Observable<List<MyItem>> search(CharSequence query) {
    return Observable.zip(
                observeNetwork(query),
                observeRealm(query),
                new Func2<List<MyItem>, List<MyItem>, List<MyItem>>() {
                    @Override
                    public List<MyItem> call(List<MyItem> networkResult, List<MyItem> databaseResult) {
                        return merge(networkResult,databaseResult);
                    }
                }
            );
}

private Observable<List<MyItem>> observeRealm(CharSequence searchString) {
    return Observable.create( /* do your realm stuff */);
}

private Observable<List<MyItem>> observeNetwork(CharSequence searchString) {
    return return Observable.create( /* do your network stuff */);
}

private List<MyItem> merge(List<MyItem> networkResult, List<MyItem> databaseResult) {
    List<MyItem> result = new ArrayList<>();
    result.addAll(databaseResult);
    for(MyItem newItem : networkResult){
        if(!databaseResult.contains(newItem)){
            result.add(newItem);
        }
    }
    return result;
}

2 个答案:

答案 0 :(得分:0)

你的问题是你没有使用json,你正在使用javascript对象数组但是试图将json字符串而不是对象推入其中

尝试

function addEstudiantes(){
    var nombre = prompt("Ingrese el nombre del estudiante: ");
    var codigo = prompt("Ingrese el codigo del estudiante: ");
    var nota = prompt("Ingrese la nota del estudiante: ");
    // create new object
    var objeto = {"Nombre":nombre,"Codigo":codigo,"Nota":nota};
    //push object into array
    estudiantes.push( objeto);
}

答案 1 :(得分:0)

如果你在js工作,你不需要像字符串那样工作,你需要用对象来处理它。

&#13;
&#13;
var linea= [];
var persona = new Object();
var objeto = new Object();
function addEstudiantes(){
    var nombre = prompt("Ingrese el nombre del estudiante: ");
    var codigo = prompt("Ingrese el codigo del estudiante: ");
    var nota = prompt("Ingrese la nota del estudiante: ");
    // create new object    
  persona.nombre=nombre;
  persona.codigo=codigo;
  persona.nota=nota;
  linea.push(persona);
  
  }
function generarjson(){
  objeto.estudiantes=linea;
  alert(JSON.stringify(objeto));
  }
  
  
&#13;
<button onclick="addEstudiantes();">Agregar estudiante</button>
<button onclick="generarjson();">Generar JSON</button>
&#13;
&#13;
&#13;