How to get values from key/values string in android? - android

After parsing JSON:
JSONArray jsonArray = new JSONArray(ResponseString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject currentObject = jsonArray.getJSONObject(i);
String local_parameter = currentObject.getString("LOCAL_PARAMETER");
}
I'm getting next string:
{"Cost": 200,
"Space": 33,
"Accommodation": 1,
"AirConditioning": 1,
"Apartment": 1,
"Campfire": 1,
"Disability": 1,
"Grill": 1,
"Kitchen": 1,
"Monument": 1,
"Parking": 1,
"Scene": 1,
"Wifi": 1,
"ClosedArea": 1 }
How to get ArrayList with key/values?
GOT SOLUTION:
Just get currentObject as JSONObject and then get a string of value what you need:
String cost = currentObject.getJSONObject("LOCAL_PARAMETER")
.getString("Cost").toString();

Just parse the JSON object one more time for the value you need.
JSONArray jsonArray = new JSONArray(ResponseString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject currentObject = jsonArray.getJSONObject(i);
String local_parameter = currentObject.getString("LOCAL_PARAMETER");
String tmp = local_parameter.get("Cost").toString(); // this gets the value of Cost
}

If you REALLY want to get an ArrayList containing your values, you can do it like this, even if I don't think this way to use JSONObjects is the best one :
final ArrayList<Pair<String, Integer>> values = new ArrayList<Pair<String,Integer>>();
final JSONObject localParams = new JSONObject(local_parameter);
final Iterator<String> iterator = localParams.keys();
while (iterator.hasNext())
{
final String key = iterator.next();
values.add(new Pair<String, Integer>(key, localParams.optInt(key)));
}
But I REALLY don't think this is the best way to handle JSONObjects...

Create an model class with the key of the json and parse it to the model class using gson.
A a = gson.fromJson(jsonRes.toString(),A.class);
here A is your model class and a is the instance of A

Related

Filter array by index and save it to another array

I´m trying to filter all values in name where id = 1 and save to an array.
This is the output I get from my WS:
[{id=1; name=Des Moines; }, {id=2; name=Cedar Rapids; }, {id=3; name=Yakima; },{id=4; name=Fort Dodge; }, {id=1; name=Iowa City; }]
if I try get property it splits my content.
Could anyone help me out on how to get the desire output?
That's JSON data, you should consider parse the data into an object , there are a lot of JSON java parsers , you could read something about GSON:
https://github.com/google/gson/blob/master/UserGuide.md
You can try something like the following
your json string must be like:
["items":{id=1; name=Des Moines; }, {id=2; name=Cedar Rapids; }, {id=3; name=Yakima; },{id=4; name=Fort Dodge; }, {id=1; name=Iowa City; }]
JSONObject jsonObject = null;
jsonObject = new JSONObject("your json string goes here!!");
String items = jsonObject.getString("items");
JSONArray array = new JSONArray(weather);
ArrayList<JSONObject > arrList = new ArrayList<JSONObject >();
for(int i = 0; i < array.length(); i++)
{
JSONObject jsonObject1 = array.getJSONObject(i);
arrList.add(jsonObject1 );
String id = jsonObject1.get("id").toString();
String name = jsonObject1.get("name").toString();
}

How to split this kind of data in android

Here is the
string one =[{"ID":5,"Name":"Sai"}]
how i get only id and name from this string
Matcher matcher = Pattern.compile("\\[([^\\]]+)").matcher(one);
List<String> tags = new ArrayList<String>();
int pos = -1;
while (matcher.find(pos+1)){
pos = matcher.start();
tags.add(matcher.group(1));
}
System.out.println("getting data"+tags);
i tried this but it didn't work
List<String> ls = new ArrayList<String>(one);
JSONArray array = new JSONArray();
for(int i = 0; i< array.length(); i++){
JSONObject obj = array.getJSONObject(i);
ls.add(obj.getString("Name"));
}
It's JSON format and it can very easily be read in Android. Here is the sample code:
JSONArray array = new JSONArray(one);
int length = array.length();
for(int i=0;i< length; i++)
{
JSONObject temp = array.getJSONObject(i);
System.out.println(temp.getString("ID"));
System.out.println(temp.getString("Name"));
}
This format of data is called JSON.
Have a look at Go to http://json.org/, scroll to (almost) the end, click on one of the many Java libraries listed.
First of all, your string initialization is wrong.
Wrong:
string one =[{"ID":5,"Name":"Sai"}]
Correct:
String one ="[{\"ID\":5,\"Name\":\"Sai\"}]";
Second, its a JSON formatted data so you can parse it using JSONArray and JSONObject classes, instead of creating any pattern.
Now, in your case its JSONObject inside JSONArray so initially create an object of JSONArray using your string.
For example:
JSONArray arrayJSON = new JSONArray(one); // 'one' is your JSON String
for(int i=0; i<arrayJSON.length(); i++) {
JSONObject objJson = arrayJSON.getJSONObject(i);
String ID = objJson.getString("ID");
.....
.....
// same way you can fetch/parse any string/value from JSONObject/JSONArray
}
it is a json formate Date
use JsonObject class to parse this data
tutorial this
JSONArray jsonArray = new JSONArray("[{\"ID\":5,\"Name\":\"Sai\"}]");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
System.out.println(object.getString("ID"));
System.out.println(object.getString("Name"));
}

Json parser in android

I have below string. i want to get jsonarray from this string keyword like id, title and so on.
Json_obj = new JSONObject(result);
vid = Json_obj.getJSONArray("id");
//vcatid= Json_obj.getJSONArray("title");
vtitle= Json_obj.getJSONArray("title");
vdesc= Json_obj.getJSONArray("content");
//vimgurl= Json_obj.getJSONArray("vimgurl");
vidlike= Json_obj.getJSONArray("likes");
vidview= Json_obj.getJSONArray("views");
//vidfblike= Json_obj.getJSONArray("vidfblike");
//vidtwlike= Json_obj.getJSONArray("vidtwlike");
storagepath= Json_obj.getJSONArray("url");
Log.i("json_Video_id vid.length() ",
"%%%%%%%%%%%%%%%%%%%%%% "
+ vid.length());
[{"id":"39","title":"\u0627\u0644\u0631\u0651\u0628\u0627 ","content":"\r\n\u0627\u0639\u0644\u0627\u0646 \u0641\u064a \u0627\u0644\u062a\u062d\u0630\u064a\u0631 \u0645\u0646 \u0645\u0634\u0643\u0644\u0627\u062a \u0627\u0644\u0631\u0628\u0627 \u0639\u0644\u0649 \u0627\u0644\u0639\u0627\u0644\u0645 \u0623\u062c\u0645\u0639.\r\n\u0641\u064a\u062f\u064a\u0648 \u0643\u0644\u064a\u0628 \u0628\u062f\u0642\u0629 \u0639\u0627\u0644\u064a\u0629 HD\r\n\u0645\u0646\u062a\u062c \u0645\u0646\u0641\u0630 \u0648\u0644\u064a\u062f \u0627\u0644\u0633\u0646\u062f New View","url":"http:\/\/www.youtube.com\/embed\/L9aPhl6eST0","views":"264","likes":"6"},{"id":"44","title":"\u062e\u064a\u0648\u0637 \u0627\u0644\u0646\u0648\u0631 \u0645\u062d\u0645\u062f \u0627\u0644\u0639\u0632\u0627\u0648\u064a ","content":"\u0641\u064a\u062f\u064a\u0648 \u0643\u0644\u064a\u0628 \u0644\u0635\u0627\u0644\u062d \u0645\u0634\u0631\u0648\u0639 \u0627\u0644\u0639\u0634\u0631 \u0627\u0644\u0623\u062e\u064a\u0631\r\n\u0643\u0644\u0645\u0627\u062a \u0633\u0644\u0637\u0627\u0646 \u0627\u0644\u0633\u0628\u0647\u0627\u0646\r\n\u0623\u062f\u0627\u0621 \u0648\u0644\u062d\u0646 \u0645\u062d\u0645\u062f \u0627\u0644\u0639\u0632\u0627\u0648\u064a\r\n\u0627\u062e\u0631\u0627\u062c \u0645\u062d\u0645\u062f \u062e\u0627\u0637\u0631\r\n","url":"http:\/\/www.youtube.com\/embed\/sOoP6SW-wuo","views":"1156","likes":"10"},{"id":"47","title":"\u062a\u0643\u0627\u0641\u0644 \u0646\u0627\u0635\u0631 \u0627\u0644\u0633\u0639\u064a\u062f ","content":"\r\n\u0644\u0644\u0641\u0642\u062f\u0650 \u0623\u0648\u062c\u0627\u0639\u064c \u062a\u062e\u062a\u0628\u0626 \u0628\u064a\u0646 \u062c\u062f\u0631\u0627\u0646 \u0627\u0644\u0623\u0644\u0645 \u0648 \u0623\u0633\u062a\u0627\u0631\u0650 \u0627\u0644\u0635\u0645\u062a .. \u0648 \u062f\u0645\u0648\u0639 \u062a\u0628\u0644\u0650\u0651\u0644\u064f \u0635\u062f\u0631\u0627\u064b \u064a\u0636\u062c\u064f\u0651 \u0628\u0627\u0644\u062d\u0646\u064a\u0646 ..\r\n\r\n\u0647\u064a \u0631\u062d\u0644\u0629\u064c \u062a\u0644\u062d\u0641\u062a\u0652 \u0628\u0623\u062d\u0632\u0627\u0646\u0650 \u0627\u0644\u063a\u0631\u0648\u0628 ..\r\n\u0648 \u062d\u0643\u0627\u064a\u0629\u064c \u0643\u064f\u062a\u0628\u062a\u0652 \u0639\u0644\u0649 \u062c\u062f\u0627\u0631\u0650 \u0627\u0644\u0642\u064e\u062f\u064e\u0631 .. \u0623\u0628\u0637\u0627\u0644\u0647\u0627 \u0623\u064a\u062a\u0627\u0645 .. \u0642\u062f \u063a\u0641\u0644 \u0639\u0646\u0647\u0645 \u0627\u0644\u0643\u062b\u064a\u064a\u064a\u064a\u0631 \u0645\u0646 \u0627\u0644\u0623\u0646\u0627\u0645 !\r\n\r\n\u0642\u062f \u0631\u0633\u0645\u0648\u0627 \u0623\u062d\u0644\u0627\u0645\u064e \u0627\u0644\u0643\u0628\u0627\u0631\u0650 \u0628\u0623\u064a\u062f\u064a\u0647\u0645 \u0627\u0644\u0645\u062a\u0639\u0628\u0629 .. \u0641\u0627\u0628\u062a\u0633\u0645\u0648\u0627 \u0628\u0634\u0641\u0627\u0647\u064d \u0634\u0627\u062d\u0628\u0629 ..\r\n\u062a\u063a\u0634\u0627\u0647\u0645 \u0645\u0633\u0627\u0621\u0627\u062a\u064c \u062a\u0628\u0643\u064a \u0627\u0646\u0643\u0633\u0627\u0631 \u0627\u0644\u062d\u064a\u0627\u0629 ..\r\n\u0648 \u064a\u0636\u0645\u0647\u0645 \u0628\u064a\u062a\u064c \u0634\u0647\u0650\u062f\u064e \u062a\u0641\u0627\u0635\u064a\u0644 \u0627\u0644\u0645\u0623\u0633\u0627\u0629 ..\r\n\u0648 \u062d\u0641\u0650\u0638\u064e \u0623\u0633\u0631\u0627\u0631 \u0627\u0644\u0643\u0631\u0627\u0645\u0629 !\r\n\r\n\r\n\u0641\u064a\u062f\u064a\u0648 \u0643\u0644\u064a\u0628 \u0633\u064a\u0646\u0645\u0627\u0626\u064a \u0645\u0646 \u0625\u0646\u062a\u0627\u062c \u0648\u0625\u0634\u0631\u0627\u0641 \u062c\u0645\u0639\u064a\u0629 \u062a\u0643\u0627\u0641\u0644 \u0627\u0644\u062e\u064a\u0631\u064a\u0629 \u0628\u0627\u0644\u0645\u062f\u064a\u0646\u0629 \u0627\u0644\u0645\u0646\u0648\u0631\u0629 takaful.sa","url":"http:\/\/www.youtube.com\/embed\/YyYmy79pKfs","views":"181736","likes":"1141"}]
This is how you parse JSON Array
JsonElement json = new JsonParser().parse(response);
JsonArray array = json.getAsJsonArray();
Iterator iterator = array.iterator();
List<CustomClass> obj_list = new ArrayList<CustomClass>();
while (iterator.hasNext()) {
JsonElement json2 = (JsonElement) iterator.next();
Gson gson = new Gson();
CustomClass obj = gson.fromJson(json2, CustomClass.class);
obj_list.add(obj);
}
Here, CustomClass will be a class with the properties you want. That means the properties you get in Array, e.g. id, title, content, likes, views, url.
Hope it helps.
The outmost container of your JSON data is an array and not an object (it start and ends with square brackets). Thus, you need to use the class JSONArray and then iterate over all elements of the array:
String jsonString = ...
JSONArray arr = new JSONArray(jsonString);
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
String id = obj.getString("id");
String title = obj.getString("title");
...
}

How to fetch Data From JSON

I have following data from JSON
{
"MenuName": "starter dish",
"SubMenu": [
"pizza dish1",
"pizza dish2"
],
"Price": [
"100",
"110"
]
},
From here i can easily fetch data from "Menuname" key as starter dish but when I fetch data from "Submenu" I get whole string as ["pizza dish1", "pizza dish2"].
Please suggest me way to differentiate both pizza dish1 and pizza dish2
Submenu and Price is not String it is JSONArray so you will need to use for-loop to get all values from Submenu JSONArray as:
JSONArray jsonsubmenu=yourjsonobject.getJSONArray("Submenu");
for(int i=0;i < jsonsubmenu.length();i++){
// get all values from jsonsubmenu JSONArray..
String str_value=jsonsubmenu.optString(i);
....
}
try this
for (int i = 0; i < jsonArray.length(); i++) {
jsonArray.getJSONObject(i).getInt("SubMenu");
}
you can use this link for creating POGO class for your response. It will automatically generate class for your response.
Use google GSON library to parse your response.
or you can simply create a JSON Array or Objects to parse it. in your case it is JSON Object or JSON arrays.
Hi There in that senarerio you have to use
JSONArray _submenu = object.getJSONArray("SubMenu");
for (int i = 0; i < _submenu.length(); i++) {
String text = _submenu.getString(i);
}
JSONArray _price = object.getJSONArray("Price");
for (int i = 0; i < _price.length(); i++) {
String text = _price.getString(i);
}
You can retrieve array values and store it in string[] and use them as per your need. i.e., as follows..
try {
JSONObject jObject = new JSONObject(jsonString);
JSONArray jSubmenu = jObject.getJSONArray("SubMenu");
subMenu = new String[jSubmenu.length()];
for (int i = 0; i < subMenu.length; i++) {
subMenu[i] = jSubmenu.getString(i);
}
JSONArray jPrice = jObject.getJSONArray("Price");
price = new String[jPrice.length()];
for (int i = 0; i < price.length; i++) {
price[i] = jPrice.getString(i);
}
} catch (Exception e) {
// TODO: handle exception
}
Just to throw in a quickie - read up on, and use GSON.
For simple small jobs I find it is the best. Not the fastest running for complex, or long structures, but really quick on the dev side.
Here's the link: google-gson

Convert JSON string in array format into an array in java

I'm trying to take the following string that I got as a json object:
[
{
"id": "picture1",
"caption": "sample caption",
"picname": "sample picture name"
}
]
and turn it into a array so I can populate a list
I've tried turning it into a jsonarray by doing this:
JSONArray myjsonarray = myjson.toJSONArray(string_containing_json_above);
but that didn't seem to work.
==============
Here is full code with the working solution
myjson = new JSONObject(temp);
String String_that_should_be_array = myjson.getString("piclist");
JSONArray myjsonarray = new JSONArray(String_that_should_be_array);
For(int i = 0; i < myjsonarray.length(); i++){
JSONObject tempJSONobj = myjsonarray.getJSONObject(i);
showToast(tempJSONobj.get("caption").toString());
}
temp is the json from the server
Issue is here:
JSONArray myjsonarray = myjson.toJSONArray(temparray);
Solution:
JSONArray myjsonarray = new JSONArray(myJSON);
// myJSON is String
Now here you are having JSONArray, iterate over it and prepare ArrayList of whatever types of you want.
here you get JSONArray so change
JSONArray myjsonarray = myjson.toJSONArray(temparray);
line as shown below
JSONArray jsonArray = new JSONArray(readlocationFeed);
and after
JSONArray jsonArray = new JSONArray(readlocationFeed);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
explrObject.getString("caption");
}
JSONArray isn't working because the JSON you provided is not an array. You can read more about JSON syntax here: http://www.w3schools.com/json/json_syntax.asp
In the meantime, you could manually create your array by paring the JSON one string at a time.
JSONObject strings = new JSONObject(jsonString);
String array[] = new String[5];
if (jsonString.has("id"){
array[0] = jsonString.getString("id");
}
if (jsonString.has("caption"){
array[1] = jsonString.getString("caption");
}
...
etc.

Categories

Resources