Skip to content Skip to sidebar Skip to footer

Parsing A Complex JSON Data Returned By API In Javascript EDITED : It Worked With PHP!CODE ADDED

EDITED WITH PHP CODE :this works $json_url = 'https://httpapi.com/api/products/reseller-price.json?auth-userid'; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)

Solution 1:

I've completely reworked my answer based on your examples / current methods. No need to go the DataTables route if you are not familiar with JS.

    <!DOCTYPE html>
<html>

<body>

<h1>Customers</h1>
<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "https://httpapi.com/api/products/reseller-price.json?5&api-key=";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var arr = JSON.parse(response);
    var key;

    //add table header
    var out = "<table style=\"border: 1px solid black;\">";
    out += "<thead><Domain><th>Add/Transfer Domain</th><th>Restore Domain</th><th>Add New Domain</th><th>Renew Domain</th></thead><tbody>";

    //loop through json keys and grab data
    for(key in arr) {
      if(arr.hasOwnProperty(key)) {

        out += "<tr><td>" +
        key +
        "</td><td>" +
        arr[key][0].pricing.addtransferdomain[1] +
        "</td><td>" +
        arr[key][0].pricing.restoredomain[1] +
        "</td><td>" +
        arr[key][0].pricing.addnewdomain[1] +
        "</td><td>" +
        arr[key][0].pricing.renewdomain[1] +
        "</td></tr>";

      }
    }

    out += "</tbody></table>";

    //put out string in defined div
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>

Solution 2:

public DataTable JsonStringToDataTable(string jsonString)
    {
        DataTable dt = new DataTable();
        string[] jsonStringArray = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");
        List<string> ColumnsName = new List<string>();
        foreach (string jSA in jsonStringArray)
        {
            string[] jsonStringData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
            foreach (string ColumnsNameData in jsonStringData)
            {
                try
                {
                    int idx = ColumnsNameData.IndexOf(":");
                    string ColumnsNameString = ColumnsNameData.Substring(0, idx - 1).Replace("\"", "");
                    if (!ColumnsName.Contains(ColumnsNameString))
                    {
                        ColumnsName.Add(ColumnsNameString);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Error Parsing Column Name : {0}", ColumnsNameData));
                }
            }
            break;
        }
        foreach (string AddColumnName in ColumnsName)
        {
            dt.Columns.Add(AddColumnName);
        }
        foreach (string jSA in jsonStringArray)
        {
            string[] RowData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
            DataRow nr = dt.NewRow();
            foreach (string rowData in RowData)
            {
                try
                {
                    int idx = rowData.IndexOf(":");
                    string RowColumns = rowData.Substring(0, idx - 1).Replace("\"", "");
                    string RowDataString = rowData.Substring(idx + 1).Replace("\"", "");
                    nr[RowColumns] = RowDataString;
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
            dt.Rows.Add(nr);
        }
        return dt;
    }

Post a Comment for "Parsing A Complex JSON Data Returned By API In Javascript EDITED : It Worked With PHP!CODE ADDED"