I want to parse this JSON and I want to display it in a pop-up window and when the user clicks any of the links it must open the URL in the browser.
This is my JSON:
[
{
"post_title": "This is an interview book of world film composers.",
"guid": "http://abcwebsite.com/news/?p=15"
},
{
"post_title": "HK Title",
"guid": "http://abcwebsite.com/news/?p=100"
},
{
"post_title": "India China Economy",
"guid": "http://abcwebsite.com/news/?p=1500"
}
]
Here is the code which I am trying: https://cutt.ly/7eVZQJm
This I will parse, but how to bring up a modal HTML popup to display all the list of stories on that popup, so that I can put all the clickable links on that popup and then when user clicks on any of the links it must open the url in a browser?
I tried this code and I am able to parse but my modal pop-up does not provide a facility to click on the ahref link
This is my code
void Start () {
// Get a reference to the World Map API:
map = WorldMapGlobe.instance;
map.OnCountryClick += (int countryIndex, int regionIndex) =>onClickOnCountry(countryIndex,regionIndex);
}
void onClickOnCountry(int countryIndex, int regionIndex){
StartCoroutine(getCountryNews(map.countries [countryIndex].name));
}
[System.Serializable]
public struct countryNewsObject
{
public string post_title;
public string guid;
}
[System.Serializable]
public class countryNewsData
{
public List<countryNewsObject > country_news_list;
}
IEnumerator getCountryNews(string country_name)
{
sModalMessage =""; //clear it initially
Debug.Log ("in getCountryNews () Posted paramter country name:"+country_name );
WWWForm form = new WWWForm();
form.AddField("country_name", country_name);
using (UnityWebRequest www = UnityWebRequest.Post("http://kitsdmcc.com/news/wp-json/custom-plugin/get_country_news_link", form))
{
Debug.Log ("getLoginedUserCountryList () Posted to get user cuntry list.." );
yield return www.SendWebRequest();
if (www.isNetworkError)
{
//errorMessage = www.error;
Debug.Log ("Json response error from WP API for UserCountryList : " + www.error );
yield return null;
}
else
{
string responseText = www.downloadHandler.text;
Debug.Log ("Json response got from WP API for country news List : " + responseText );
//parse json response:
JSONNode jsonMainNode = SimpleJSON.JSON.Parse(responseText);
// get individual values from the jsonNode for the data node
string JSONToParse = "{\"country_news_list\":" + responseText.ToString() + "}";
countryNewsData data = JsonUtility.FromJson<countryNewsData>(JSONToParse);
Debug.Log ("COUNT :"+data.country_news_list.Count);
if(data.country_news_list.Count>0){
Debug.Log (" get list of news title for this country..." );
foreach (countryNewsObject countryNews in data.country_news_list) {
Debug.Log ("Json Response - News Title:"+countryNews.post_title+" News Link: "+countryNews.guid );
string sNewsTitle = countryNews.post_title;
string sHyperLinkofNews = "<a href="+countryNews.guid+">"+countryNews.post_title+"</a>";
sHyperLinkofNews = sHyperLinkofNews.Replace('"', ' ').Trim();//to get the string without double quotes
string sMessage = sNewsTitle+ " \n"+sHyperLinkofNews+" \n";
sModalMessage =sModalMessage+sMessage ;
}
//Call model pop up and add the sModalMessage which is fetched from the json using the above function
HFTDialog.MessageBox("News Title and News Links of "+country_name, sModalMessage, () => { Application.Quit(); });
Debug.Log ("Json Response - Final Message to Pop- up:"+sModalMessage);
}else{
HFTDialog.MessageBox("News Title and News Links of "+country_name, "Currently No News Available ", () => { Application.Quit(); });
}
}
}
}
//End of get country news function
Code for the modal pop -up is
using UnityEngine;
using System;
// example:
// HFTDialog.MessageBox("error", "Sorry but you're S.O.L", () => { Application.Quit() });
public class HFTDialog : MonoBehaviour {
Rect m_windowRect;
Action m_action;
string m_title;
string m_msg;
static public void MessageBox(string title, string msg, Action action)
{
GameObject go = new GameObject("HFTDialog");
HFTDialog dlg = go.AddComponent<HFTDialog>();
dlg.Init(title, msg, action);
}
static public void CloseMessageBox(GameObject go)
{
Destroy(go.gameObject);
}
void Init(string title, string msg, Action action)
{
m_title = title;
m_msg = msg;
m_action = action;
}
void OnGUI()
{
const int maxWidth = 640;
const int maxHeight = 480;
int width = Mathf.Min(maxWidth, Screen.width - 20);
int height = Mathf.Min(maxHeight, Screen.height - 20);
m_windowRect = new Rect(
(Screen.width - width) / 2,
(Screen.height - height) / 2,
width,
height);
m_windowRect = GUI.Window(0, m_windowRect, WindowFunc, m_title);
}
void WindowFunc(int windowID)
{
const int border = 10;
const int width = 50;
const int height = 25;
const int spacing = 10;
Rect l = new Rect(
border,
border + spacing,
m_windowRect.width - border * 2,
m_windowRect.height - border * 2 - height - spacing);
GUI.Label(l, m_msg);
Rect b = new Rect(
m_windowRect.width - width - border,
m_windowRect.height - height - border,
width,
height);
if (GUI.Button(b, "close"))
{
Destroy(this.gameObject);
m_action();
}
}
}
[![Modal pop-up window][1]][1]
Also how can I make the modal popup more darker so that my text will be more visible