Parse xml From WebService To Json (Important)


This is very important: Funny story, I was developing an app and I needed to pass data from a website's  Web Service to an app. This is the only way to get access to the database data from an Ionic Cordova app because Javascript does not have inbuilt functions to connect to the database. In this case, you will have to build a Web Service, sort of a middle layer that communicates with the database. Most of the Web Services return data as XML instead of JSON which is really lightweight and easy to get access to the objects. The problem is that sometimes in JavaScript/TypeScript it is really hard to parse XML to JSON, there is an error that gets thrown when trying to pass XML to JSON. The solution is to convert XML or request data to be in form of TEXT in Http Headers you pass to the Web Service. 

When you define or specify the type of Data you want the Web Service to return in an AJAX API call, then Stringify that data returned, then parse it into JSON. Funny fact: Make sure when you replace , make sure you leave out '[' and ']'. This is the only way JSON will know that is an object. It took me 2 days or even longer, because the first time I solved this problem, I did not document it.  The code is shown below: It's a messy code but you get the idea.


//This function will call web api shungarts
CallShungArtsAPI(){
$.ajax({

url: "http://192.168.1.75:81/ShungArtsWebService.asmx/RetrieveUserPostsForDefaultWithAPI_key",
type: 'GET',
data: {_apiKeypi:'PutAPI_KeyHere'},

dataType: 'Text',
//dataType:'Text',
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//var mt = data.toString()
console.log(data)
var cleanData = data.toString().replace('','')
var Stripped = cleanData.toString().replace('','')
//var j = JSON.stringify(Stripped)
//console.log(md)
// console.log(j)
var LastStripp = Stripped.replace('','')
console.log(LastStripp);
var StringF = JSON.stringify(LastStripp)
var J = JSON.parse(StringF)
var k = $.parseJSON(J)
console.log(k)

//var m = JSON.stringify(data)
//console.log(m)
// var me = $.parseJSON(m)
// var mk = JSON.parse(JSON.stringify(data));
//var k = JSON.parse(mk)
//JavaScriptSerializer().Serialize(H)
//var yourval = jQuery.parseJSON(JSON.stringify(data));
//console.log(me)
// console.log('_______________')
// console.log(m)
//console.log('_______________')
//console.log(mk)

}
,
error: function (xhr, textStatus, errorThrown) {

console.log('Error in Operation:=> ' + textStatus +': '+ errorThrown);

}

})
}




Network
published
v.0.01





For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email Address[email protected]