0

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

Jerry Abraham
  • 103
  • 12
  • And what have you tried so far? Also please limit your question to one issue at a time .. currently there are multiple subjects: "How to parse JSON in c#/Unity?", "How to display clickable links in Unity?", "How to open a URL in an external browser in Unity?" ... Also one more question: you want that happening in the Editor or in build game on runtime? – derHugo Nov 25 '19 at 05:40
  • @derHugo I am new to Unity just 1 week now started learning and trying to do a POC and u might be experienced developer in Unity and my questions might be silly and simple to you – Jerry Abraham Nov 25 '19 at 05:46
  • No it isn't silly or simple .. it just does not comply with the community rules for [asking a good question](https://stackoverflow.com/help/how-to-ask). I just gave you three huge hints for subjects you should look into yourself. If you then have a specific coding related issue while implementing these don't hesitate to come back with a question providing what you tried and what exactly didn't work as expected. – derHugo Nov 25 '19 at 05:50
  • @derHugo i tried this - but I am not reaching on this function getCountryNews() can u check what I am doing wrong – Jerry Abraham Nov 25 '19 at 05:56
  • Not without seeing your code, no. – derHugo Nov 25 '19 at 05:58
  • @derHugo modified the question please check the link – Jerry Abraham Nov 25 '19 at 06:00
  • Your question should include the relevant code *in the question itself.* Ideally, you should provide a [mre] showing us where exactly you are stuck. – tripleee Nov 25 '19 at 06:16
  • So so far you downloaded the JSON string .. then I guess the next thing to do for you would be looking into the first step e.g.: ["How to parse JSON in Unity"](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity) – derHugo Nov 25 '19 at 06:18
  • @derHugo I am able to parse now with no issues But my modal pop does not supporting clickable url I Want to open the URL in the browser when user clicks the story link – Jerry Abraham Nov 25 '19 at 07:08
  • Sorry, I don't know what `HFTDialog` is or who implemented it... – derHugo Nov 25 '19 at 07:11
  • @derHugo do u know any other pop-up box dialogue which can support ahref link,I am searching but still could not find any good one – Jerry Abraham Nov 25 '19 at 07:15

0 Answers0