JSON parsing into ListView Android - android

This is the JsonObject i get form parsing my url: {"status":0,"items":"1333333454510|-7611868|-7222457"
now i need to get the seperate numbers seperetly as long.
i also get an error if i try to convert it to string: org.json.JSONException: Value 1333333454510|-7611868|-7222457 at items of type java.lang.String cannot be converted to JSONObject
How? Thanks
here is some code
JSONObject obj = JSONParser.getJSONFromUrl(URL + "search", parameters);
String firstItem = obj.getJSONObject("items").toString();

Split your items using \\|.
Example :
String items= "1333333454510|-7611868|-7222457" ; //here you got items
String[] _items=items.split("\\|");
Now iterate loop and convert it into long using Long.ParseLong().
EDIT :
As Ashwin said you need to use json.getString("items"); to get items.
You can convert it into long using following code.
ArrayList<Long> longs=new ArrayList<Long>();
for(int i=0;i<_items.length;i++){
longs.add(Long.parseLong(_items[i]));
}

Use Split function to separate.
parse json to get items in items String
use jsonObject.getString instead of jsonObject.getJSONObject to avoid error.
String items= "1333333454510|-7611868|-7222457" ;
String[] itemsArray =items.split("\\|");

String response= "{"status":0,"items":"1333333454510|-7611868|-7222457"}" ; // ie, response contains the string.
JsonObject json= new JsonObject(response);
String item=json.getString("items");
String [] items =item.split("|");
Will get 3 numbers in the items array.

JsonObject json= new JsonObject(response);
String item=json.getString("items");
String [] items =item.split("|");

your problem is here
JSONObject obj = JSONParser.getJSONFromUrl(URL + "search", parameters);
String firstItem = obj.getJSONObject("items").toString();
so change it to
String firstItem = obj.getString("items").toString();
and then do split operations as suggested by others.
SO force always with you:)

Related

Convert object a JSON object to string

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");

Parsing json code to a string in android using volley

i need help to parse this json code to actual strings using android volley. this is the json code:[{"name":"Tayo","0":"Tayo","thread_name":"Welcome","1":"Welcome","post":"Hi there,","2":"Hi there,","post_time":"Sunday","3":"Sunday"},{"name":"Pete","0":"Pete","thread_name":"Welcome","1":"Welcome","post":"Hi,am pete","2":"Hi,am pete","post_time":"Monday","3":"Monday"}].
I have tried other helps but not working. Thanks!
Remember:
if the .json content starts with { is considered as a Json Object.
if the .json content starts with [ is considered as a Json Array.
so you hava a JsonArray then you can parse your content like this way:
//Obtain the JsonArray
JSONArray jsonArray = new JSONArray(myJsonContent);
// Get the JsonObjects inside JsonArray
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonobject = jsonArray.getJSONObject(i);
}
// strData is the json data received.
JSONArray jsonArray = new JSONArray(strData);
for (int i=0; i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name");
String zero = jsonObject.optString("0");
String thread_name = jsonObject.optString("thread_name");
String one = jsonObject.optString("1");
String post = jsonObject.optString("post");
String two = jsonObject.optString("2");
String post_time = jsonObject.optString("post_time");
String three = jsonObject.optString("3");
//Just an example
arrayName.add(jsonObject.optString("name"));
}
You can even use array to store data instead of the string.

How to parse a compact JSON Array in android

I have a JSON Array like this:
[4, 3, 2, 1,...]
It is a simple array, and I am not able to find a single tutorial that tells me how to parse this array. I want to assign each element to a string Array that I have created like:
String H[];
I always get stuck when it comes to returning a value from a method, I get confused. For Example:
Within the Oncreate() method, after initializing all variables I type:
solarData();
//displaying only one value from the String Array H[];
TextView.setText(H[0]);
public JSONArray solarData() throws JSONException{
//After getting response from HttpClient and getting result
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray,length();i++){
String H[i] = jArray.getJSONObject(i).toString();
}
return jArray;
}
Now what is it returning? I want it to return the String Array. So that I can display any value I want from the String Array to which I am storing these values.
From the JSONArray documentation, you can see there is a constructor that takes a valid JSON string and does all of the parsing work for you. You seem to have identified this already.
The problem here is that in each iteration of your for loop, you are attempting to create a new String array. You should initialize your array before the loop, and then populate its contents after initializing it. Something like this should work for you:
public JSONArray solarData() throws JSONException{
JSONArray jArray = new JSONArray(result);
// consider renaming H to something meaningful (e.g. planetList)
String H[] = new String[jArray.length()];
for (int i=0; i < jArray,length() ;i++) {
H[i] = jArray.getJSONObject(i).toString();
}
return jArray;
}

How to parse JSON response?

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.

Get JSONObject by ID from another JSONObject - Android

I have a little issue with parsing JSON String which I get from a web server. So my JSON looks something like this :
{
..........
"statistics":{
"660":{
"param_id":660,
"cat_id":2,
"param_title":"Number",
"param_value":"26",
"value_type":"singleline",
"elem_order":"1"}
,
"662":{
"param_id":662,
"cat_id":2,
"param_title":"Name",
"param_value":"Dani",
"value_type":"singleline",
"elem_order":"2"
}
// --||--
}
}
So I get a JSONObject statisics and I want to get JSONObjects from statistics, but the problem is that their name is different everytime.So I can't just do json.getJSONObject("660");. So any suggestions how can I do that?
You can do something like this :
if(jsonObj.has("statistics")){
Iterator<Object> keys = stats.keys();
while(keys.hasNext()){
String key = (String) keys.next();
JSONObject obj = new JSONObject();
obj = stats.getJSONObject(key);
// get JSON
}// end while
}//end if
use JSONObject.keys() to get key iterator, then getJsonObject( key) to get object.
Try below code-
JSONArray nameArray = statisics.names();
JSONArray valArray = statisics.toJSONArray(nameArray);
where names() method returns array of names and it is stored in nameArray and valArray contains array of values corresponding to valArray.
You can iterate using for loop over these array to get values.
use like this
JSONObject jsonOnb = json.getJSONObject("statistics") ;
JSONObject pagesObj = jsonOnb.getJSONObject(jsonOnb.names().getString(0));
Log.i("pageid", "pageid= " +pagesObj.get("param_id"));
Log.i("title", "title= " +pagesObj.get("cat_id"));
..............

Categories

Resources