Jadvaldagi xotirjam usul java

Bu xato nima uchun menga kimdir aytib berishi mumkinmi?

Server Log:

 StandardWrapperValve[ws_site.ApplicationConfig]: Servlet.service() for servlet ws_site.ApplicationConfig threw exception
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 5
at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
    at com.google.gson.Gson.fromJson(Gson.java:791)

Javascript funksiyasi, to'ldirilgan ariza ma'lumotlarini olish va serverga jo'natish uchun mas'ul:

function save()
{

    var str_name = $("#name").val();
    var str_email = $("#email").val();


    var str_country = $("#country").val();
    var str_state = $("#state").val();
    var str_city = $("#city").val();
    var str_zipcode = $("#zipcode").val();
    var str_neighborhood = $("#neighborhood").val();
    var str_street = $("#street").val();
    var str_number = $("#number").val();


    var objdata = '{"email_user":"' + str_email + '","name_user":"' + str_name}';
var objlocation = '{"country":"' + str_country + '","state":"' + str_state + '","city":"' + str_city + '","neighborhood":"' + str_neighborhood + '","street":"' + str_street + '","number":"' + str_number + '","zipcode":"' + str_zipcode + '"}';

    var obj = '{"user":['+objdata+'],"endereco":['+objlocation+']}';

    $.ajax({
        headers: {'content-type': 'application/json'},
        dataType: 'json',
        method: "POST",
        url: "http://localhost:8080/SystemExample/webservice/Save/data",
        data: obj
     }).done(function (data)
     {
        alert(data);
     });
}

Rahat Java usuli:

@POST
@Path("data")
@Produces(MediaType.TEXT_PLAIN)
@Consumes({MediaType.APPLICATION_JSON})
public String registerUser(Gson json)  
{
    User u = json.fromJson("user", User.class);
    Address a = json.fromJson("endereco", Address.class);
    u.setAddress(a);
    userDAO.save(u);
    return "Saved successfully!";
}

UserDAO usuli saqlansin:

    public void save(User u) {
    EntityManager em = JPAUtil.getEntityManager();
    EntityTransaction tx = em.getTransaction();

    try {
        tx.begin();
        if (u.getId_User() == null) {

            em.persist(u);
        } else {
            em.merge(u);
        }
        tx.commit();
    } catch (Exception ex) {
        ex.printStackTrace();
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
    } finally {
        em.close();
    }
}

Jsonni ob'ektga aylantirish uchun Gsondan foydalanish

0
Yon suzingiz bir oz qisqa ko'rinadi, endi chiziqlar yo'qmi?
qo'shib qo'ydi muallif Steve Smith, manba

6 javoblar

Siz serverga ob'ektni yubormaysiz, siz faqat magistralni yuborasiz:

var obj = '...';

Buning o'rniga, ob'ektni yuborish:

var objdata = {
  "email_user": str_email,
  "name_user": str_name
};
var objlocation = {
  "country": str_country,
  "state": str_state,
  "city": str_city,
  "neighborhood": str_neighborhood,
  "street": str_street,
  "number": str_number,
  "zipcode": str_zipcode
};

var obj = {
  "user": [objdata],
  "endereco": [objlocation]
};

Ob'ektni hali ham mag'lubiyatga aylantirgan ko'rinishidagi satr.

2
qo'shib qo'ydi
Ha: juda toza
qo'shib qo'ydi muallif Maurice Perry, manba

Siz serverga ob'ektni yubormaysiz, siz faqat magistralni yuborasiz:

var obj = '...';

Buning o'rniga, ob'ektni yuborish:

var objdata = {
  "email_user": str_email,
  "name_user": str_name
};
var objlocation = {
  "country": str_country,
  "state": str_state,
  "city": str_city,
  "neighborhood": str_neighborhood,
  "street": str_street,
  "number": str_number,
  "zipcode": str_zipcode
};

var obj = {
  "user": [objdata],
  "endereco": [objlocation]
};

Ob'ektni hali ham mag'lubiyatga aylantirgan ko'rinishidagi satr.

2
qo'shib qo'ydi
Ha: juda toza
qo'shib qo'ydi muallif Maurice Perry, manba

objdata to'g'ri json sifatida joylashtirilmadi. Buni ko'ring:

    function save() {
        var str_name = $("#name").val();
        var str_email = $("#email").val();
        var str_country = $("#country").val();
        var str_state = $("#state").val();
        var str_city = $("#city").val();
        var str_zipcode = $("#zipcode").val();
        var str_neighborhood = $("#neighborhood").val();
        var str_street = $("#street").val();
        var str_number = $("#number").val();

        var objdata = '{"email_user":"' + str_email + '","name_user":"' + str_name + '"}';
        console.log(objdata);

        var objlocation = '{"country":"' + str_country + '","state":"' + str_state + '","city":"' + str_city + '","neighborhood":"' + str_neighborhood + '","street":"' + str_street + '","number":"' + str_number + '","zipcode":"' + str_zipcode + '"}';
        console.log(objlocation);

        var obj = '{"user":[' + objdata + '],"endereco":[' + objlocation + ']}';
        console.log(obj);

        $.ajax({
            headers: {'content-type': 'application/json'},
            dataType: 'json',
            method: "POST",
            url: "http://localhost:8080/SystemExample/webservice/Save/data",
            data: JSON.parse(obj)
        }).done(function (data) {
            alert(data);
        });
    }
1
qo'shib qo'ydi
JSON.parse() usuli, json sifatida aniq ko'rsatilganidek, kerak emas.
qo'shib qo'ydi muallif Ravi MCA, manba

objdata to'g'ri json sifatida joylashtirilmadi. Buni ko'ring:

    function save() {
        var str_name = $("#name").val();
        var str_email = $("#email").val();
        var str_country = $("#country").val();
        var str_state = $("#state").val();
        var str_city = $("#city").val();
        var str_zipcode = $("#zipcode").val();
        var str_neighborhood = $("#neighborhood").val();
        var str_street = $("#street").val();
        var str_number = $("#number").val();

        var objdata = '{"email_user":"' + str_email + '","name_user":"' + str_name + '"}';
        console.log(objdata);

        var objlocation = '{"country":"' + str_country + '","state":"' + str_state + '","city":"' + str_city + '","neighborhood":"' + str_neighborhood + '","street":"' + str_street + '","number":"' + str_number + '","zipcode":"' + str_zipcode + '"}';
        console.log(objlocation);

        var obj = '{"user":[' + objdata + '],"endereco":[' + objlocation + ']}';
        console.log(obj);

        $.ajax({
            headers: {'content-type': 'application/json'},
            dataType: 'json',
            method: "POST",
            url: "http://localhost:8080/SystemExample/webservice/Save/data",
            data: JSON.parse(obj)
        }).done(function (data) {
            alert(data);
        });
    }
1
qo'shib qo'ydi
JSON.parse() usuli, json sifatida aniq ko'rsatilganidek, kerak emas.
qo'shib qo'ydi muallif Ravi MCA, manba

Server uchunmoningizdan JSON ma'lumotlariga ulanishga harakat qilmoqdasiz.

User u = json.fromJson("user", User.class);
Address a = json.fromJson("endereco", Address.class);

Bu kod user va endereco degan ma'noni anglatadi, quyida kabi JSON moslamalari bo'lishi kerak.

{
    "user":{
        "email_user":"str_mail","name_user":"nameeee"
    },
    "endereco":{
        "country":"str_country","state":"str_state","city":"str_city","neighborhood":"str_neighborhood","street":"str_street","number":"str_number","zipcode":"str_zipcode"
    }
}

Lekin sizning holingizda foydalanuvchi va endereco aslida JSONArray (kvadrat qavatlarga qarang).

{
    "user":[
        {
            "email_user":"str_mail",
            "name_user":"nameeee"
        }
    ],
    "endereco":[
        {
            "country":"str_country",
            "state":"str_state",
            "city":"str_city",
            "neighborhood":"str_neighborhood",
            "street":"str_street",
            "number":"str_number",
            "zipcode":"str_zipcode"
        }
    ]
}

Quyidagi satrni o'zgartiring

var obj = '{"user":['+objdata+'],"endereco":['+objlocation+']}';

uchun

var obj = '{"user":'+objdata+',"endereco":'+objlocation+'}';
0
qo'shib qo'ydi
@DeividSantosAlves Nima deb aytishga harakat qilayotganingizni bilmasangiz
qo'shib qo'ydi muallif Ravi MCA, manba
Men aytgandek qildim, u erda paydo bo'ldi: Grave: 1-qator: 0 "u" Grave: 1-satr: 1 belgisi mavjud muqobil emas, balki Grave: layn 1: 2 'e' Grave: layn 1: 3 'r' belgisida mavjud muqobil emas Grave: layn 1: 4 '%' belgisida mavjud alternativ mavjud emas, faqat bitta tilimga qo'ydim. Lekin bu faqat paydo bo'ladi.
qo'shib qo'ydi muallif Deivid Santos Alves, manba

Server uchunmoningizdan JSON ma'lumotlariga ulanishga harakat qilmoqdasiz.

User u = json.fromJson("user", User.class);
Address a = json.fromJson("endereco", Address.class);

Bu kod user va endereco degan ma'noni anglatadi, quyida kabi JSON moslamalari bo'lishi kerak.

{
    "user":{
        "email_user":"str_mail","name_user":"nameeee"
    },
    "endereco":{
        "country":"str_country","state":"str_state","city":"str_city","neighborhood":"str_neighborhood","street":"str_street","number":"str_number","zipcode":"str_zipcode"
    }
}

Lekin sizning holingizda foydalanuvchi va endereco aslida JSONArray (kvadrat qavatlarga qarang).

{
    "user":[
        {
            "email_user":"str_mail",
            "name_user":"nameeee"
        }
    ],
    "endereco":[
        {
            "country":"str_country",
            "state":"str_state",
            "city":"str_city",
            "neighborhood":"str_neighborhood",
            "street":"str_street",
            "number":"str_number",
            "zipcode":"str_zipcode"
        }
    ]
}

Quyidagi satrni o'zgartiring

var obj = '{"user":['+objdata+'],"endereco":['+objlocation+']}';

uchun

var obj = '{"user":'+objdata+',"endereco":'+objlocation+'}';
0
qo'shib qo'ydi
@DeividSantosAlves Nima deb aytishga harakat qilayotganingizni bilmasangiz
qo'shib qo'ydi muallif Ravi MCA, manba
Men aytgandek qildim, u erda paydo bo'ldi: Grave: 1-qator: 0 "u" Grave: 1-satr: 1 belgisi mavjud muqobil emas, balki Grave: layn 1: 2 'e' Grave: layn 1: 3 'r' belgisida mavjud muqobil emas Grave: layn 1: 4 '%' belgisida mavjud alternativ mavjud emas, faqat bitta tilimga qo'ydim. Lekin bu faqat paydo bo'ladi.
qo'shib qo'ydi muallif Deivid Santos Alves, manba
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb