Display nested data from JSON in textview android - android

I think this problem is trivial, but I am not advanced in programming. I have problem with displaying Json data to TextView
My Json file is like
{
"person":{
"firstName":"John",
"lastName":"Jankow",
"nick":"Jayjay"
}
...
}
another data from Json like address, id etc. displaying without problem, when I use
((TextView) findViewById(R.id.person)).setText("Person: " + json.optString("person"));
I get string "person":{
"firstName":"John",
"lastName":"Jankow",
"nick":"Jayjay"
}"
I've tried to do this using
((TextView) findViewById(R.id.person)).setText("Person: " + json.optString("firstName") + json.optString("lastName"));
but the result is empty TextView.
Question ==> Is there any method to handle this data and display correctly like substring or something like this?

do this
JSONObject person = json.getJSONObject("person");
((TextView) findViewById(R.id.person)).setText("Person: " + person.getString("firstName") + person.getString("lastName"));

Not entirely sure if this helps but see this.
JSONObject person = jsonObj.getJSONObject("person");
String firstName = person.getString("firstName");
String lastName = person.getString("lastName");
Use strings in an awesome way.
textView.setText(firstName);

Related

Display JsonArray from api and sort

I am trying to display data from a rest api containing a Json Array and a list of Json objects. I tried following a tutorial and was able to display the data on textview. What I actually want to do is gather the data in a Hashmap in terms of key and value pair and sort them accordingly.
public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
if(!response.isSuccessful()){
textView.setText("Code: " +response.code());
return;
}
int j=0;
List<Post> posts = response.body();
Map<Integer,String> data = new HashMap<Integer, String>();
// Iterating through post and saving data in content variable
for(Post post: posts)
{
String content ="";
//String name = post.getName();
if(!(post.getName()==(null)) && !(post.getName().equals("")))
{
data.put(post.getListid(),post.getName()); //For inserting data into hashmap
content += "ListID: " + post.getListid() + "" + "\n";
content += "Name: " + post.getName() + "" + "\n";
}
textView.append(content);
}
The current code saves the data in a content variable and displays it in the text view.
I am relatively new to using hashmap and retrofit for displaying data. I would really appreciate if someone could explain me where I am going wrong with my code. I tried printing out the data but was only able to print the last line and not the rest.
This is my Json file that I want to sort and display the data
[{"listId":2,"name":null},{"listId":2,"name":"Item 990"},{"listId":2,"name":null},{"listId":2,"name":null},{"listId":2,"name":""},{"listId":4,"name":"Item 771"},{"listId":2,"name":""}]
This is the code that I am using to displaying all my json data in textview
for(Map.Entry entry: data.entrySet())
{ textView.append(entry.getKey()+ " Value:" +entry.getValue() +"\n");}
I if understand correctly, you have a List<Post> posts that you want to sort out ? Why don't you sort the posts as you need and save them accordingly? You could just remove the post from your list if their name are null or ""

How to get Text written after "," in MultiAutoCompleteTextView?

I am developing an application that uses MultiAutoCompleteTextView for showing hints in the drop down list.In this application I retrieve the value written in the MultiAutoCompleteTextView by using
multitextview.getText();
and then query this value to server to recieve JSON response which is shown as suggestions in the drop down list.
If a user types Mu and then Selects music from the list and then types box for another suggestion the content in the MultiAutoCompleteTextView becomes Music,box and now the value for querying to the server is Music,box instead of this I want to select only box.
My question is how to retrieve text written after "," in MultiAutoCompleteTextView?
Can this be achieved using getText()?
I solved this issue
String intermediate_text=multitextview.getText().toString();
String final_string=intermediate_text.substring(intermediate_text.lastIndexOf(",")+1);
I'm sure there are several ways to get around this. One way to do it would be:
String textToQuerryServer = null;
String str = multitextview.getText().toString(); // i.e "music, box" or "any, thing, you , want";
Pattern p = Pattern.compile(".*,\\s*(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
textToQuerryServer = m.group(1);
System.out.println("Pattern found: "+ textToQuerryServer);
}else {
textToQuerryServer = str;
System.out.println("No pattern: "+ textToQuerryServer);
}

How to remove brackets and quotation marks from JSON String

I have a ArrayAdapter the places JSON strings into a list view. The problem is when it places the strings into the textview it leaves brackets and quotation marks. For example, if the string contained the name Bob. The string would show up in the ListView as ["Bob"]. How do I remove the brackets and quotation marks?
Here is what I use to get the JSON strings,
String username = json2.getString(KEY_USERNAME);
String number = json2.getString(KEY_NUMBER);
String content = json2.getString(KEY_COMMENT);
tempList2.add (new Item (username, number, content));
customAdapter.addAll(tempList2);
You can use a simple replace:
String bob = "[\"Bob\"]";
bob = bob.replace("[", "").replace("\"", "").replace("]", "");
Log.i("Test", bob);
This will now just print Bob
As an addon to Phil's answer, the end quote (in my chrome browser) did not get removed. I fixed it by using .replace("\"", "") again at the end.

need to retrieve data from the database

I have a very long string in the database that needs to be retrieved into a swipe view.
But,the problem is that the string comprises of set of "\n\n"
Whenever it is separated with this expression i need to put it in another slide,i mean i am using SWIPE view here..
if(tablecolumn==\\n\\n)
{
code to break it to parts
}
Is this how i should be doing it?
If i am wrong,how to break this string to different parts and enable it into SWIPE VIEW in to different swipe view?
You can simply break your string comprising of a special character like this :-
String str ="mynameisjhon.yournameisdash.bla";
, here you have a string concatenated with " . " (period character)
to break this string do this :-
StringTokenizer st = new StringTokenizer(str, "."); //break the string whenever "." occurs
String temp =st.nextToken(); // it will have "my name is jhon" break
String temp2 = st.nextToken();// it will have "your name is dash"
String temp3 = st.nextToken();//it will have "bla"
now your string is breaked into parts!
Anything else?
Load the whole string into your ViewAdapter and seperate it via substring
or load the string in your Activity/Fragment seperate it via substring, put the strings in an ArrayList, an initiate your ViewAdapter with the ArrayList as data source
either way use substring

how to add a data to webservice with special characters?

i am trying to add a two strings to the web service its working fine when i send through browser and it returns "inserted successfully"
but when i run my application its displaying an error as E/Responce(17407): Illegal character in query at index 74: http://purple2.com/beerbro/addgroupon.php?userid=27&data=100001190851696,1|100001640732983,1|100002430763518,1|100002332633534,1|100000549596039,1|1375802933,1|1587585991,1|1569563632,1|100000132426450,1|100004296815391,1|1519195978,1
,
i think there is a problem with "|" this but i am not able to find the solution
here is the code
for(int i=0; i
data=data+friendid[i]+","+status[i]+"|";
data.replaceAll("","%20");
if(i==0){
}
else if(i==10)
{
String res=UrltoValue.getValuefromUrl(DataUrls.addfriendsurl+"?userid="+usid+"&data="+data.substring(0,data.length()-1));
Log.e("res",DataUrls.addfriendsurl+"?userid="+usid+"&data="+data.substring(0,data.length()-1));
Log.e("Responce",res);
data="";
}
if '|' is the problem then use URLEncoder and encode the String
String data = URLEncoder.encode("100001190851696,1|100001640732983,1|100002430763518,1|100002332633534,1|100000549596039,1|1375802933,1|1587585991,1|1569563632,1|100000132426450,1|100004296815391,1|1519195978");
String mainURL = "http://purple2.com/beerbro/addgroupon.php?userid=27&data="+data;
Hope this helps you.

Categories

Resources