I have Json, I want to make it so that a button is created on the ID, and when I clicked on it, the browser opens and goes to the link that is listed in json, I’m just a beginner, google all, and nothing is clear. I'm doing for android in Android studio. Here is an example of JSON:

{"id":1,"link":"http:vk.com","name":"VK"} 
  • It is not entirely clear what you mean by "what would a button be created by ID", that is, how many id so many buttons? or what? - DevOma
  • Yes, that is, there is each id, its own button - Shufler3

2 answers 2

This is a regular Json object. Take it and parse it.

 String json = "{\"id\":1,\"link\":\"http://www.vk.com\",\"name\":\"VK\"}"; JSONObject obj = new JSONObject(json); String url = obj.getString("link"); String name = obj.getString("name"); 

You now have a link and text for the button. Find the button. We put the program text and write the listener.

 Button go = (Button) findViewById(R.id.button); go.setText(name); go.setOnClickListener(new OnClickListener() { @Override public void onClick(View v){ Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); } }); 

Read this article. There everything about JSON painted.

UPD

About the list - everything is simple. So let's go. First, JSON the JSON array.

 String json = "[{\"id\":1,\"link\":\"http://www.vk.com\",\"name\":\"VK\"}, {\"id\":1,\"link\":\"http://www.google.com\",\"name\":\"Google\"},{\"id\":1,\"link\":\"http://www.yandex.ru\",\"name\":\"Yandex\"}"; JSONArray array = new JSONArray(json); int size = array.length(); String[] links = new String[size]; String[] names = new String[size]; for(int n = 0; n < size; n++) { JSONObject obj = array.getJSONObject(n); links[n] = obj.getString("link"); names[n] = obj.getString("name"); } 

Everything, we have arrays with links and site names. Next we need to create a ListView in the markup.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:id="@+id/list" android:layout_height="wrap_content" android:layout_width="wrap_content"/> </LinearLayout> 

Now create an adapter and fill the list with data.

 ListView list = (ListView) findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(this)); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names); listView.setAdapter(adapter); 

And the listener for the list.

 list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(links[position-1])); startActivity(i); } }); 

We use the position of the clicked item (list item) as an index for an array of links.

FULL CODE

MainActivity.java

 import android.app.*; import android.os.*; import org.json.*; import android.widget.*; import android.widget.AdapterView.*; import android.content.*; import android.net.*; import android.view.*; import android.util.*; public class MainActivity extends Activity { String[] links, names; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView list = (ListView) findViewById(R.id.list); String json = "[{\"id\":1,\"link\":\"http://www.vk.com\",\"name\":\"VK\"}, {\"id\":1,\"link\":\"http://www.google.com\",\"name\":\"Google\"},{\"id\":1,\"link\":\"http://www.yandex.ru\",\"name\":\"Yandex\"}]"; try{ JSONArray array = new JSONArray(json); int size = array.length(); links = new String[size]; names = new String[size]; for(int n = 0; n < size; n++) { JSONObject obj = array.getJSONObject(n); links[n] = obj.getString("link"); names[n] = obj.getString("name"); } } catch (JSONException e){ Log.d("$", e.toString()); } ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names); list.setAdapter(adapter); list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(links[position])); startActivity(i); } }); } } 

main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="top"> <ListView android:id="@+id/list" android:layout_height="match_parent" android:layout_width="match_parent"/> </LinearLayout> 

As I understand it, you don’t know the basic things in development, so I’ll give you some good learning sites.

StartAndroid

Alexander Klimov website

To learn, you only need to read blogs and practice a lot. Google It is most important. Everything you need is already there. We can only understand all this.

Oh yes, screenshot:

enter image description here

Happy coding!

  • I get from the server, so there are a lot of id, etc. - Shufler3
  • Then you need to create a ListView and list it. Do you need buttons? With the list it will look karyavo, I advise without them. Add an answer? - Flippy
  • It should be with the buttons, then I just have to connect it with a telly, that is, that I would not manually link on the remote, but simply click on the button and open the link, but then I need to open it at least on the phone, Thanks for the link. I read - Shufler3
  • I can add the answer. Need to? - Flippy
  • I think you need. - Shufler3
 String json = "{\"id\":1,\"link\":\"http:vk.com\",\"name\":\"VK\"}"; LinearLayout layout = (LinearLayout)findViewById(R.id.layout); //layout в который поместить кнопку try { JSONObject rootJSON = new JSONObject(new JSONTokener(json)); String name = rootJSON.getString("name"); String link = rootJSON.getString("link"); Button button = new Button(this); button.setText(name); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link))); } }); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) layout.addView(button, params); } catch (JSONException e) { e.printStackTrace(); } 
  • And in the layout folder, in the XML file, what should I register? And if I have Json on the server, how? - Shufler3
  • And yet, let's say I have a few id, link, name, then how do I get here? - Shufler3
  • In this answer, the button is created programmatically and is designed for one id. For a set of buttons of unknown quantity there are lists - Flippy
  • @ Shufler3 "in the layout folder, in the XML file" you need to write LinearLayout; "let's say I have several id, link, name" this code can be put into the method and given to the input name and link, everything of course depends on your requirements. - katso
  • @katso How do I do this? - Shufler3