i'm trying to create a list of objects from a JSON file but my result is always NULLenter image description hereenter image description here
Problem is on this line
JSONObject json = new JSONObject("listBeer.json");
You have pass you JSON string here, not the file name. See documentation here. Your code will thow JSONException, since string listBeer.json is not a valid JSON.
Related
I have a Json String like below
["Monday","Tueday","wednesday","Thuesday","Friday","Saturday"]
What I want to do is send this Json String to a web server using the android studio volley library.
SO I keep getting this error
com.android.volley.ParseError: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
The string cannot be converted to JsonArray.
So I have 2 questions I need help with.
My first is how to convert string to JsonArray. For example below String.
["Monday","Tueday","wednesday","Thuesday","Friday","Saturday"]
My second question is about passing this Jason Array (once we create) to a web server. I was researching about the Hashmap but I don't know how to send the JsonArray in Hashmap.
I had a go with below code, but it did not work.
#Override
protected Map<String, String> getParams() throws AuthFailureError {
return super.getParams();
}
I don't know how to send JsaonArray in hashmap.
So Overall, I would like to send this above string to web server.
Supose that the name of string is myString.
rString = myString.replace('[','');
rString = rString.replace(']','');
rString = rString.replace('"','');
String split[] = rString.split(',').
Now you have an array called split with every position is a day.
Edit: This is for frist question
Use that
JSONArray yourJsonArray = JSONArray.fromObject(yourJson);
Answer to first question:
I think it's not the exact response from the server, and there's a HTML <br> tag in the response.
because the line Value <br of type java.lang.String cannot be... clearly contains string <br, try replacing the HTML tags and/or decoding/unescaping the HTML tags to plain text, AND then you can convert that plain JSON text to JSONArray.
this snippet might help you out with decoding the response:
String parsedReponse = Html.fromHtml(serverResponse).toString()
if (parsedReponse != null) parsedReponse = parsedReponse.trim();
Answer to second question:
check out OkHttp3 and Retrofit libraries, they're pretty popular and amazing.
those can help you send POST data to your server
hope that helped you :)
Im trying to get Data from a JSONObject that contains an array of user details
Im trying to get the username from this json
JSON Data
I can get the top level data easily with
JSONObject obj = response.getJSONObject(i);
Comment saving = new Comment();
saving.set_id(obj.getString("_id"));
saving.setDetail(obj.getString("details"));
saving.setVotes(obj.getInt("votes"));
How do i get the username value?
Thanks
The username key is again nested inside the JSON object you have in obj.
What you can do is
String username = obj.getJSONObject("user").getString("username");
Try this :
obj.getJSONObject("user").getString("username");
In yours IDE if you will press ctrl+space after obj with a dot (.) operator , you should get auto fill with various option to select .
the basic difference between JSONObject and JSONArray is following :
JSONArray starts with []
JSONObject starts with {}
As you can see the user is a JSONObject , hence obj.getJSONObject("user").getString("username");
I prefer decoding JSON like this instead of using third party library
I am working on an Android project where I am using volley to parse the data from JSON. I am able to parse the data if it is an array by following this tutorial. But when I try to parse a single object using getJSONObject, it is returning null. I want to get the value of that particular object.
This is my JSON file:
In the above file I want to retrieve only responseInfo which is a JSON object.
You need to create JSONObject
JSONObject object = new JSONObject(responce);
//get responce code
String query = object.getString("responceCode");
Please use Gson library for json parsing
https://github.com/google/gson
I know this question has been asked a number of times but I didn't find any relative answer of my question.
I am trying to read json data from asset folder, but I'm getting following exception while getting
I searched number of stuffs but didn't help. Please give me any reference or hint.
Thanks in Advance.
Use
String searchedTerm = jsonObject.getString(TAG_SEARCHEDTERM);
JSONArray results = jsonObject.getJSONArray(TAG_RESULTS);
instead of
JSONObject searchedTerm = jsonObject.getJSONObject(TAG_SEARCHEDTERM);
JSONArray results = searchedTerm.getJSONArray(TAG_RESULTS);
because TAG_SEARCHEDTERM is key-value pair instead of JSONObject and you are trying to cast a String value to JsonObject.
I guess you need to get the Dish Name where you are getting exception.
You can get the dish name shown below...
String searchedTerm = jsonObject.getString(TAG_SEARCHEDTERM);
Using this
JSONArray results = jsonObject.getJSONArray(TAG_RESULTS);
you will get the "results" JSONArray as shown in your json file.
and you can iterate through it using for loop.
i have this object in a Json file and i succeded at parsing many objects but i couldn't parse this one :
"advertiser_marker_geocoord": "33.848463,-7.033653"
how can i parse it using java?
// Here json is assumed to be a json object accompanied from response you would be getting.
String value = json.getString("advertiser_marker_geocoord");
now split values on behalf of , String arr[] = value.split(",");
If you want me share exact code.. please share the exact json that you are getting