I try parsing a single json object and i have a problem.
this is my object :
{"id":"16","nickname":"vvvv","country":"Albania","date":"2013-03-25 21:08:40","best":"8","updateDate":"1364243187.162"}
i use this code:
JSONObject object = (JSONObject) new JSONTokener(responce).nextValue();
String query = object.getString("nickname");
i get java.langClassCastExeption java.jangBoolean
simply create an JSONObject from given string and then extract all values from jsonObject as :
//create json object form responce string
JSONObject object = new JSONObject(responce);
//get nickname
String query = object.getString("nickname");
///.... get other value for object
and also use JSONObject.has(String name) for checking is current json object contain mapping for given name or not
Related
I'm using amirdew/JSON library, which you can find here to parse my string to JSON.
The string I retrieved is this: https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Portugal
This is the code I have at the moment, and it is not working and I believe that it is because of the keys...
public void ParseJson (String json){
JSON json2 = new JSON(json);
String firstTag = json2.key("query").key("pages").key("extract").stringValue();
txtInfo = findViewById(R.id.txtInfo);
txtInfo.setText(firstTag);
}
The firstTag variable is null because it can't retrieve any value. I want to retrieve the text inside "extracts". How can I do it? What keys do I need?
I suggest you to use the JSONObject which is already inside the SDK. You would use it like this:
String input = "..."; // your input
JSONObject obj = new JSONObject(input);
Strings extracts = obj.getJSONObject("query").getJSONObject("pages").getJSONObject("23033").getString("extract");
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");
To Store JSON object to String is possible. But Is there any way to store the JSON Object as it is in Shared Preferences and retrieve too?
JSONObject data_obj = new JSONObject();
JSONArray arr_obj = new JSONArray();
JSONObject main_obj = new JSONObject();
data_obj.put("id", "1");
data_obj.put("name", "Loin");
arr_obj.put(data_obj);
main_obj.put("user_review", arr_obj);
How will main_obj be store in Shared Preferences?
You can convert JsonObject to String and put that string in Shared preference and can do reverse to get JsonObject.
A JSONObject is an unordered collection of name/value pairs. Its
external form is a string wrapped in curly braces with colons between
the names and values, and commas between the values and names.
For example,
String myString = new JSONObject().put("JSON", "Hello, World!").toString();
print(myString) it produces the string {"JSON": "Hello, World"}.
and for reverser you can do
JSONObject myJSON = new JSONObject(myString);
For SharedPreference you can refer
Reference
JSONObject is ultimately a key/value pairs string, so convert it into the string using toString() and save it!
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...
}
}
How can I parse json from the URL? below is my json structure which does not has tags.
[{"channelId":"0465CDBE","channelName":"ATV2"},{"channelId":"06E6923B1","channelName":"Phoenix"},{"channelId":"07B4FB7ed","channelName":"N24"},{"channelId":"115B73E39","channelName":"ORF2"},
Simply get the JSONArray
JSONArray jArr = new JSONArray(jsonString);
for(int i=0;i<jArr.length;i++)
{
String jChannel = jArr.getJSONObject(i).getString("channelId");
String jChannelName = jArr.getJSONObject(i).getString("channelName");
//you can now play with these variables or add to some list or do whatever you like.
}
The outer object is a JSONArray, so you can iterate it with a "for".
Inside your json array, you have simple json objects. You can parse it by key or iterate the keys.
You can user json object and get the value with getJSONArray
examples:
testob = {"channelId":"07B4FB7ed","channelName":"N24"}, {"channelId":"115B73E39","channelName":"ORF2"},
JSONObject jsonObject = new JSONObject(testob);
JSONArray dataArray = jsonObject.getJSONArray("data");
JSONObject jsonProductData = dataArray.getJSONObject(0);