0

I've my JSON like this:

{
  "shops": [
    {
          "id": "831",
          "name": "18 and East",
          "categories": [
            "1",
            "12",
            "13"
          ],
          "locations": [
            {
              "lat": "53.403297",
              "lng": "-2.978689",
              "address": "Bold Street Liverpoool, L1 4EA"
            },
            {
              "lat": "51.590111",
              "lng": "-0.146134",
              "address": "58 Fortis Green Road, London, N10 3HN"
            },
            {
              "lat": "53.406738",
              "lng": "-2.981188",
              "address": "137-139 Market Square  Liverpool"
            }
          ],
          "image": "5574-18-and-East-sale.jpg"
        }
    ....

And I have Shop.java, Response.java and Locations.java like this (this files gets the data as objects):

Now I want to parse the address into following file, I tried several ways but I'm not able to parse the address from array. And I'm able to parse id, name and image.

can someone helpme to solve this problem?

Kabe
  • 223
  • 2
  • 4
  • 16
  • what is the error the Gson Library is throwing to you? – Hector Sanchez Sep 11 '13 at 16:33
  • Don't know if helps, but instead of ArrayList I think Gson expects List instead – Hector Sanchez Sep 11 '13 at 16:33
  • also the `ArrayList` categories; should be `String[]` – Hector Sanchez Sep 11 '13 at 16:41
  • I copy your code - and debug it - everything works! I have parsed address. Check link and look at bottom in debugable panel [link](http://rendergame.wordpress.com/?attachment_id=358) – Dariusz Mazur Sep 11 '13 at 16:52
  • The above code works fine. I want to know how to parse address in hashmap. Please provide some code – Kabe Sep 11 '13 at 17:40
  • @daro2189: In your link, I couldn't see that you are parsing address. Can you provide detailed picture or code? – Kabe Sep 11 '13 at 17:43
  • Could you be more specyfic - which address you want to parse to hashmap? I see that you create list with hashmap, and you add some items into it – Dariusz Mazur Sep 11 '13 at 17:50
  • I want to parse address in locations in json string. **{ "lat": "53.406738", "lng": "-2.981188", "address": "137-139 Market Square Liverpool" }** See my post above. – Kabe Sep 11 '13 at 18:06

1 Answers1

0

I attach my code:

Add your class Location.class, Shop.class and Response.class - no changes

//Code which parse json put it in your "doInBackground"
try {
        String str = getString(R.string.data);
        Gson gson = new Gson();
        Response response = gson.fromJson(str, Response.class);

        if(response != null){
           //TODO response is not null
           Log.e("", "ADDRESS: " + response.shops.get(0).getLocations().get(0).address);
        }

} catch (Exception e){
    e.printStackTrace();
}

//in strings.xml I addes json string which I parsed

<string name="data">
    {
    \"shops\": [
    {
    \"id\": \"831\",
    \"name\": \"18 and East\",
    \"categories\": [
    \"1\",
    \"12\",
    \"13\"
    ],
    \"locations\": [
    {
    \"lat\": \"53.403297\",
    \"lng\": \"-2.978689\",
    \"address\": \"Bold Street Liverpoool, L1 4EA\"
    },
    {
    \"lat\": \"51.590111\",
    \"lng\": \"-0.146134\",
    \"address\": \"58 Fortis Green Road, London, N10 3HN\"
    },
    {
    \"lat\": \"53.406738\",
    \"lng\": \"-2.981188\",
    \"address\": \"137-139 Market Square  Liverpool\"
    }
    ],
    \"image\": \"5574-18-and-East-sale.jpg\"
    }]}

</string>

After parsing in response exists parsed 'address', please check it :)

My log: 09-11 20:16:17.765: D/(2610): ADDRESS: Bold Street Liverpoool, L1 4EA

============= Update Add this function into shop class:

public ArrayList<String> getShopAddress(){
   ArrayList<String> shoopAddress = new ArrayList<String>();
   if(locations == null){
     //there is no address
     return shoopAddress;
   }

   for(Locations l : locations){
      final String addr = l. getAddress();
      if(addr != null)
        shoopAddress.add(addr);
    }
   return shoopAddress;
}

Now you can get all address which are in shops by calling:

response.shops.get(0).getShopAddress(); //and it returns all address (as strings list) assigned to shop
Dariusz Mazur
  • 587
  • 5
  • 21
  • Thanks for your answer. But with this code:response.shops.get(0).getLocations().get(0).address I'm only able to get one address only. but I want to get all addresses related to the shops. Note: one shop has more than one address. – Kabe Sep 11 '13 at 18:53
  • Yap, this is what I asked for. but it's return several error at: ArrayList
    and shoopAddress.add(addr);
    – Kabe Sep 11 '13 at 19:15
  • Check right now - I corrected. Error is because instead of "string" list I created address list – Dariusz Mazur Sep 11 '13 at 19:17
  • hi daro2189, I've one more question. how could i group lat, lng and address – Kabe Sep 11 '13 at 19:51
  • What do you mean - group lat, lng and address? Would you like to group it as address?? – Dariusz Mazur Sep 11 '13 at 19:59
  • Please check and answer this question: http://stackoverflow.com/questions/18750314/parse-json-objectslat-and-lng-in-googlemap-as-markers – Kabe Sep 11 '13 at 20:11