I am taking result of a json webservice in JSONObject. While printing this jsonObject it is printing exact result. I got problem at the time of fetching values in this result because i am reading a complex response that is in the form of
{"FoodMenuRS"
:{"Status":"Success",
"TotalResults":2,
"Results":{"Items":
{"Item":[
{"#Id":"6","#Name":"Tea"},
{"#Id":"4","#Name":"Coffee"}
]}}}}
Here i am reading through,
JSONArray jsonArray = json.getJSONArray("Item");
Here i am getting error "No Value for Item"
Where as i got fetched value while calling another service which is simple in format,
{"earthquakes":
[{"eqid":"c0001xgp","magnitude":8.8,"lng":142.369,"src":"us","datetime":"2011-03-11 04:46:23","depth":24.4,"lat":38.322},
{"eqid":"2010xkbv","magnitude":7.5,"lng":91.9379,"src":"us","datetime":"2010-06-12 17:26:50","depth":35,"lat":7.7477}]}
I called it using,
JSONArray earthquakes = json.getJSONArray("earthquakes");
Please help how to fetch this type of Json response. Thanks in advance.
ya it is very complex but i refer you to use Gson library to parse Json as it is parse in structural manner example
check this:
http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
Done with
JSONObject menuObject = jObject.getJSONObject("menu");
String attributeValue = menuObject.getString("value");
private String jString = "{\"menu\": {\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
JSONObject menuObject = jObject.getJSONObject("menu");
String attributeId = menuObject.getString("id");
String attributeValue = menuObject.getString("value");
JSONObject popupObject = menuObject.getJSONObject("popup");
JSONArray menuitemArray = popupObject.getJSONArray("menuitem");
With every new curley braces call getJSONObjectand for child call getString.
Or you can follow Gson concept to fetch response.
Related
I'm trying to convert a json object to a string using but I'm getting 'No Value for NAMES'. My code is as follows:
JSONObject jsonObject = new JSONObject(resp);
String c = jsonObject.getString("NAME");
msg("" + c);
Currently my object is as follows:
{"Names":[{"NAME":"Haircut"},{"NAME":"Blowdry"},{"NAME":"styling "},{"NAME":"treatment "},{"NAME":"braiding"}]}
How can I convert this data so that I may ingest the data into a listview dynamically.
Any help will be highly appreciated.
Names is and array in your JSON. So, firstly your should get it. Try this one:
JSONArray names = (JSONArray)jsonObject.get("Names");
((JSONObject) names.get(0)).get("NAME");
The following JSON starts with an array type which I am having a lots of problem to Parse. Please help.
[{"type":"1","name":"ABC","start_date":"5","end_date":null,"time":"00:00:00","description":"abc","venue":"","v_id":"1","c1name":"","c1phno":"","c1email":"","c2name":"","c2phno":"","c2email":""}]
Please help me out if possible.
Are you trying to get the data as a JSONArray?
It would look like this:
String json = "your json data";
JSONArray jsonArray = new JSONArray(json);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String type = jsonObject.getString("type");
String name = jsonObject.getString("name");
And so on...
I am using the Retrofit package from square to make http request to my server. I get json data from my server that needs to be parsed. The problem I'm having is when the name field has more than one word I get a "Unterminated object at character". What can be the problem.
This works fine
{results=[{id=23.0, name= Canada}]}
This does not
{results=[{id=23.0, name= United States}]}
JSONObject jsonResponse = new JSONObject(data);
JSONArray result = jsonResponse.getJSONArray("results");
for(int i=0; i <result.length();i++ )
{
MyObj obj = new MyObj();
obj.id= result.getJSONObject(i).optString("id").toString();
obj.name=result.getJSONObject(i).optString("name").toString();
p.add(obj);
}
Your input is not valid according to the JSON specs.
It shoud be:
{"results":[{"id":23.0, "name":"United States"}]}
See: http://json.org/
i have two situation of Json output .
one is data that found and i have a json array and a json object like this:
{"data":"yes"}[{"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},{"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]
other situation is that data not found :
{"data":"no"}
just one json object.
how parse this data in android client for support two situtaion?
First, you should validate your json in http://jsonlint.com/ if you test it you will look that is a wrong json. So, for make it right, in your server your response should look something like this:
{"data":"yes","response":[{"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},{"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]}
And in that case, in android
JSONObject jsonObj = new JSONObject(response);
if (jsonObj.getString("data").compareTo("yes") == 0) {
JSONArray jsonArray = jsonObj.getJSONArray("response");
//To-Do another code
}
and that's all
Here is a possible case: (you need to fix your json format)
Success -
string resultJSON =
{"success":true,
"data":[
{"id":"10","number":"7","text":"text7","desc":"text7_again","user_code":"0"},
{"id":"11","number":"8","text":"text8","desc":"text8_again","user_code":"1"}]}
Failed -
string resultJSON =
{"success":false}
Then
JSONObject jsonRoot = new JSONObject(resultJSON);
bool isSuccess = jsonRoot.getBoolean("success");
if (isSuccess) {
// do the array parser
for(int i=0; i<jsonData.lenght;i++) {
JSONObject jsonObj = jsonData.getJSONObject(i);
String id = jsonObj.getString("id"); // get the value of id
String desc = jsonObj.getString("desc"); // and so on...
}
}
I have called webservice and got a response as below please tell me how can I parse it.....
FillAutoCompleteBudgetMasterItemsByMasterIdResponse{
FillAutoCompleteBudgetMasterItemsByMasterIdResult=anyType{
string=Agrochemicals; string=Certification fee; string=Consultation;
string=Contracts; string=Electricity; string=Fertilizers; string=Fuel;
string=Implements and Equipments; string=Insurance; string=Irrigation and Water;
string=Labours; string=Machinery usage; string=Marketing; string=Other Items;
string=Post Production; string=Repairs and Maintenance; string=Seeds/Seedlings ;
string=Services; string=Training; string=Transportation; }; }
This not a valid Response data.Beacuse it should contain a (key,value).
By using key we get the value.
JSONArray arObjects = new JSONArray(Respone);for(int i = 0; i < arObjects.length(); i++)
JSONObject jOb = arObjects.getJSONObject(i);
String date = jOb.getString("PublishedDate");
String price = jOb.getString("introduction");
This is not a valid json. The strings are not quoted.
http://json.org/example.html
this is not valid json format.. you cant parse using Json... if the string is not in the valid json format it throws an exception ...
The String in enclosed [] braces in called josn array..
The String in enclosed {} braces in called josn object..
in general json array contain josn objects
JSONArray array= new JSONArray(jsonString);
for(i=0;i< array.length ;i++){
JSONObject result = new JSONObject(array.get(i));
}
This tutorial might help you to parse the json in an easy and hassle free way. Please check this out.