Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 272
Registered: ‎05-24-2010
My Device: DEV Alpha C & BB 9720
My Carrier: Idea and Airtel

Json parser

can any one help me how to use json to parse json response from a http connection..

i want to parse the following one..what i want is i have to store the string value (bolded one) ..

 

 

{
  "status": "OK",
  "results": [ {
    "types": [ "natural_feature" ],
    "formatted_address": "Vinayaka Sagar, LB Nagar, Warangal, Andhra Pradesh, India",
    "address_components": [ {
      "long_name": "Vinayaka Sagar",
      "short_name": "Vinayaka Sagar",
      "types": [ "natural_feature" ]
    }, {
      "long_name": "LB Nagar",
      "short_name": "LB Nagar",
      "types": [ "neighborhood", "political" ]
    }, {
      "long_name": "Warangal",
      "short_name": "Warangal",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Warangal",
      "short_name": "Warangal",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Andhra Pradesh",
      "short_name": "Andhra Pradesh",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "India",
      "short_name": "IN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 17.9925265,
        "lng": 79.6116069
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 17.9819139,
          "lng": 79.5955995
        },
        "northeast": {
          "lat": 18.0031385,
          "lng": 79.6276143
        }
      },
      "bounds": {
        "southwest": {
          "lat": 17.9888510,
          "lng": 79.6096963
        },
        "northeast": {
          "lat": 17.9986929,
          "lng": 79.6174210
        }
      }
    }
  }, {
    "types": [ "locality", "political" ],
    "formatted_address": "Warangal, Andhra Pradesh, India",
    "address_components": [ {
      "long_name": "Warangal",
      "short_name": "Warangal",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Warangal",
      "short_name": "Warangal",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Andhra Pradesh",
      "short_name": "Andhra Pradesh",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "India",
      "short_name": "IN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 17.9952778,
        "lng": 79.6116667
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 17.9370920,
          "lng": 79.5688248
        },
        "northeast": {
          "lat": 18.0299137,
          "lng": 79.6611186
        }
      },
      "bounds": {
        "southwest": {
          "lat": 17.9370920,
          "lng": 79.5688248
        },
        "northeast": {
          "lat": 18.0299137,
          "lng": 79.6611186
        }
      }
    }
  }, {
    "types": [ "administrative_area_level_2", "political" ],
    "formatted_address": "Warangal, Andhra Pradesh, India",
    "address_components": [ {
      "long_name": "Warangal",
      "short_name": "Warangal",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Andhra Pradesh",
      "short_name": "Andhra Pradesh",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "India",
      "short_name": "IN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 17.9744395,
        "lng": 79.9192702
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 17.2667278,
          "lng": 78.8893890
        },
        "northeast": {
          "lat": 18.5079356,
          "lng": 80.7096863
        }
      },
      "bounds": {
        "southwest": {
          "lat": 17.2667278,
          "lng": 78.8893890
        },
        "northeast": {
          "lat": 18.5079356,
          "lng": 80.7096863
        }
      }
    }
  }, {
    "types": [ "administrative_area_level_1", "political" ],
    "formatted_address": "Andhra Pradesh, India",
    "address_components": [ {
      "long_name": "Andhra Pradesh",
      "short_name": "Andhra Pradesh",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "India",
      "short_name": "IN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 17.0477624,
        "lng": 80.0981869
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 12.6288580,
          "lng": 76.7210780
        },
        "northeast": {
          "lat": 19.8384760,
          "lng": 84.7874150
        }
      },
      "bounds": {
        "southwest": {
          "lat": 12.6288580,
          "lng": 76.7210780
        },
        "northeast": {
          "lat": 19.8384760,
          "lng": 84.7874150
        }
      }
    }
  }, {
    "types": [ "country", "political" ],
    "formatted_address": "India",
    "address_components": [ {
      "long_name": "India",
      "short_name": "IN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 20.5936840,
        "lng": 78.9628800
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 6.4627000,
          "lng": 68.1097000
        },
        "northeast": {
          "lat": 35.5071581,
          "lng": 97.3955550
        }
      },
      "bounds": {
        "southwest": {
          "lat": 6.4627000,
          "lng": 68.1097000
        },
        "northeast": {
          "lat": 35.5071581,
          "lng": 97.3955550
        }
      }
    }
  }, {
    "types": [ "colloquial_area", "political" ],
    "formatted_address": "Southern Asia",
    "address_components": [ {
      "long_name": "Southern Asia",
      "short_name": "Southern Asia",
      "types": [ "colloquial_area", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 25.0376400,
        "lng": 76.4563087
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": -7.4891118,
          "lng": 60.5170005
        },
        "northeast": {
          "lat": 38.4894403,
          "lng": 97.3955550
        }
      },
      "bounds": {
        "southwest": {
          "lat": -7.4891118,
          "lng": 60.5170005
        },
        "northeast": {
          "lat": 38.4894403,
          "lng": 97.3955550
        }
      }
    }
  }, {
    "types": [ "natural_feature" ],
    "formatted_address": "Asia",
    "address_components": [ {
      "long_name": "Asia",
      "short_name": "Asia",
      "types": [ "natural_feature" ]
    } ],
    "geometry": {
      "location": {
        "lat": 34.0478630,
        "lng": 100.6196553
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": -12.9403000,
          "lng": 25.0159000
        },
        "northeast": {
          "lat": 81.6691780,
          "lng": -168.3545000
        }
      },
      "bounds": {
        "southwest": {
          "lat": -12.9403000,
          "lng": 25.0159000
        },
        "northeast": {
          "lat": 81.6691780,
          "lng": -168.3545000
        }
      }
    }
  } ]
}

Developer
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Json parser

Do you know how JSON works? I think you need to read a little more about. 

 

 "status": "OK",
  "results": [ {
    "types": [ "natural_feature" ],
    "formatted_address": "Vinayaka Sagar, LB Nagar, Warangal, Andhra Pradesh, India",
    "address_components": [ {
      "long_name": "Vinayaka Sagar",
      "short_name": "Vinayaka Sagar",
      "types": [ "natural_feature" ]
    }

 

In this case, results is an json array. So first extract that array using the key and once you have the json array extracted you can get the object using the key "formatted_address". 

 

 

You also need the json library which is widely available.

Developer
Posts: 272
Registered: ‎05-24-2010
My Device: DEV Alpha C & BB 9720
My Carrier: Idea and Airtel

Re: Json parser

thanks for the replay

 

i have read something about JSON ..

 

if anything starts with "{" that will be a JSONobject...

is that right...

 

or shall i continue with jsonArray..

 

 

thanks regards yakub pasha 

Developer
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Json parser

 

"result":
{
"uid":980,
"level":"USER",
"firstname":"asdasd",
"lastname":"asdasd",
"login":"+1222222222",
"password":"evaretuha",
"email":"asdasd@hotmail.com",
"number_country_code":"CA",
"spoken_language":"fr-FR",
"connector":0,
"mobile_number":"+1222222222",
"asr":"FALSE",
"creation_date":"2010-05-10 10:46:16",
"last_view":"2010-05-13 09:17:18"
}
}

 

The above is a typical Json object,

 

 

 JSONObject bb = new JSONObject(ContactresponseString); //the variable passed here is the json object shown above
        String result = "error";
        try {			
        	result = bb.getString("result");        	
		} catch (JSONException e) {				
			try {
				JSONObject error = bb.getJSONObject("error");
				throwJSONError(error);				
			} catch (JSONException e1) {
				e1.printStackTrace();
			}					
		} 

 

 

That's typically how you do it. However I don't know if it's the best way. 

 

 

 

Developer
Posts: 272
Registered: ‎05-24-2010
My Device: DEV Alpha C & BB 9720
My Carrier: Idea and Airtel

Re: Json parser

i already did the same thing but am getting exception...

i dont have JSONError class 

so i did

 

try {
                
                JSONObject result = new JSONObject(finalresponse);
                
                String si=result.getString("formatted_address");
                Dialog.alert(si);

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

Developer
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Json parser

 

JSONArray temp = musicConn.getTracks(); //similar to response string

	for(int curRecord=0;curRecord<temp.length();curRecord++){
			
				try {														
					JSONObject val = temp.getJSONObject(curRecord);
					
					String val1 = val.get("title");		
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}			

 

Thats how u read an array. 

 

Make sure that the JSON stream is correct, otherwise it won't work

 

Developer
Posts: 272
Registered: ‎05-24-2010
My Device: DEV Alpha C & BB 9720
My Carrier: Idea and Airtel

Re: Json parser

sorry dude i dint find any method with name getTracks();

can u tell me wat is the type of musicConn??

i dint get u?

 

JSONArray temp = musicConn.getTracks(); //similar to response string

Developer
Posts: 178
Registered: ‎08-26-2009
My Device: Not Specified

Re: Json parser

That's the json stream....It's just a string

Developer
Posts: 272
Registered: ‎05-24-2010
My Device: DEV Alpha C & BB 9720
My Carrier: Idea and Airtel

Re: Json parser

thank your very much dude...

 

instead of using JSON i used saxparser to parse the data..

i am getting confuse while using Json..

 

thank you very much.,.

New Contributor
Posts: 7
Registered: ‎06-30-2010
My Device: Storm 9500
My Carrier: Vodafone

Re: Json parser

HI,

 

I'm parsing json data and facing the problem of json.jar file not being valid.

 

I tried to work on sax parser as well, but went on with JSON.

 

I wanna know how did you convert the response from JSON to be used in the SAX parser.

 

is it something like this..

 

 public void parse()
         {
            
             try{
                 XMLReader parser = XMLReaderFactory.createXMLReader();
                 parser.setContentHandler(this);
                 InputStream is = getClass().getResourceAsStream("msg");
                 InputSource iss = new InputSource(is);
                 parser.parse(iss);
             } catch (IOException e) {
                    System.out.println(e.getMessage());
                } catch (SAXException e) {
                    System.out.println(e.getMessage());
                }
            }
         public void startElement1(String nsURI, String strippedName,String tagName, Attributes attributes)
            throws SAXException {
            
            if(tagName.equalsIgnoreCase("FirstName")) fname = false;
        else if(tagName.equalsIgnoreCase("LastName")) lname = true;
        else if(tagName.equalsIgnoreCase("UserAddress")) address = true;
        else if(tagName.equalsIgnoreCase("CityName")) city = true;
        else if(tagName.equalsIgnoreCase("Phone1")) mobile = true;
        else if(tagName.equalsIgnoreCase("AgencyId")) agencyId = true;
        else if(tagName.equalsIgnoreCase("UserID")) userId = true;
        else if(tagName.equalsIgnoreCase("Currency")) currency = true;
        else if(tagName.equalsIgnoreCase("Amount")) amount = true;

        }

Please tell me how can I parse as  the response is in JSON.

 

I will be comfrtable with JSON or SAX.

 

Thanks