Object Syntax Problems in Json?

Asked

Viewed 426 times

1

I’m having trouble in syntax of shoppingsObj, why are you making this mistake?

Expected a JSON Object, array or literal.

shoppingsObj = {

    "shoppings": [
        { "nome": "Bangu Shopping", "licenseKey":"0", "acessKey":"0", "secretKey":"0", "latitude1":["-22.879832"], "latitude2":["-22.877738"], "longitude1:":["-43.468601"], "longitude2:":["-43.465978"] },
        { "nome": "Boulevard Shopping Campos", "licenseKey":"0", "acessKey":"0", "secretKey":"0", "latitude1":["-21.755484"], "latitude2":["-21.753139"], "longitude1:":["-41.350870"], "longitude2:":["-41.346417"] }
    ]

 }
  • Your JSON has an invalid format. You are manipulating it with javascript?

  • 1

    Hello Thiago. Where does this come from? Ajax request? Are you using jQuery or another framework? Did you write this manually in your . js? Could you explain exactly how you got this JSON?

  • @Marcelodeandrade, I’m manipulating with java for Android. I need to create an object that is the shoppingsObj and then do the following shoppingsObj.latitude[] = "";

  • @Guilhermenascimento wrote here above now.

  • It is possible to post the whole object, or at least a little more complete?

  • 1

    Wait, so you’re writing in Java then? I guess this syntax will only work with the Json package and doing the Parse of a string. Could give details of the code?

Show 1 more comment

2 answers

2


I am manipulating with java for Android. I need to create an object that is the shoppingsObj and then do the following shoppingsObj.latitude[] = ""

If you’re using that JSON, the valid format for your case is:

{
    "shoppings": [{
            "nome": "Bangu Shopping",
            "licenseKey": "0",
            "acessKey": "0",
            "secretKey": "0",
            "latitude1": ["-22.879832"],
            "latitude2": ["-22.877738"],
            "longitude1:": ["-43.468601"],
            "longitude2:": ["-43.465978"]
        },
        {
            "nome": "Boulevard Shopping Campos",
            "licenseKey": "0",
            "acessKey": "0",
            "secretKey": "0",
            "latitude1": ["-21.755484"],
            "latitude2": ["-21.753139"],
            "longitude1:": ["-41.350870"],
            "longitude2:": ["-41.346417"]
        }
    ]

}

But to get the result, as you exemplified is not possible. You have an object shoppings containing a array, you will need to access one of the indexes or iterate on it.

An example would be:

shoppingsObj.shoppings[0].nome // retornará "Bangu Shopping"
shoppingsObj.shoppings[0].latitude1 // retornará "-22.879832"
  • 1

    thanks. Now I need to know how I consume it in java for Android, I will create a topic asking this.

0

Try it like this:

{"shoppings": [
    { "nome": "Bangu Shopping", "licenseKey":"0", "acessKey":"0", "secretKey":"0", "latitude1":["-22.879832"], "latitude2":["-22.877738"], "longitude1:":["-43.468601"], "longitude2:":["-43.465978"] },
    { "nome": "Boulevard Shopping Campos", "licenseKey":"0", "acessKey":"0", "secretKey":"0", "latitude1":["-21.755484"], "latitude2":["-21.753139"], "longitude1:":["-41.350870"], "longitude2:":["-41.346417"] }
]}

Note that this json is an object that has a list of objects called shoppings then for you to change the latitude of the first mall, you would have to do:

shoppingsObj.shoppings[0].latitude = ...
  • "shoppingsObj" is not a String is an object.

  • I need it to do the following shoppingJb.latitude[1] = "";

  • But in the example I gave you he is exactly an object. In q language you are doing?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.