Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
grenadedylex
Posts: 73
Registered: ‎04-08-2014
My Device: Z10
My Carrier: XL
Accepted Solution

how to replace this "\" string on json webservice?

[ Edited ]

hi

i have problem with this json

"{\"search_result\":[{\"name\":\"Mall Summarecon\",\"category\":\"BusinessEntity\",\"id\":\"1\"},{\"name\":\"Bamboo Dim Sum\",\"category\":\"BusinessEntity\",\"id\":\"2\"},{\"name\":\"Dimsum Ceker\",\"category\":\"Item\",\"id\":\"1\"}]}" 

i want to replace that "\" string

the json are already working on my app through .cpp file

i already add json.replace on my qml 

function simpleSearch(response){
        indicator.stop()
        model.clear()
        console.log("Response: "+response)
        var json = JSON.parse(response) 
        json = json.replace('\\', ' ') // this is how the way i replace       
        if (json == "[]")
            notFound.visible = true
            
        else 
            model.append(json.search_result)
    }

 but still doesnt work

is there any different way to replace it?

thanks

Please use plain text.
Developer
grenadedylex
Posts: 73
Registered: ‎04-08-2014
My Device: Z10
My Carrier: XL

Re: how to replace this "\" string on json webservice?

anybody can help? thanks

Please use plain text.
Developer
anditsung
Posts: 233
Registered: ‎09-30-2010
My Device: Z10, Onyx2
My Carrier: IND XL

Re: how to replace this "\" string on json webservice?

Why want to replace it?

"\"" will add " to the string

 

Please use plain text.
Developer
Developer
oGG
Posts: 55
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel

Re: how to replace this "\" string on json webservice?

[ Edited ]

you can treat the JSON response as a  string into JS.

 

var res = "";
for(var i = 0; i < response.length; i++) {
     res = res + response[i].replace("/", "");
}

 

...and after make PARSE.

My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! :smileyhappy:
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! :smileyhappy:
Please use plain text.
Developer
grenadedylex
Posts: 73
Registered: ‎04-08-2014
My Device: Z10
My Carrier: XL

Re: how to replace this &quot;\&quot; string on json webservice?

still doesnt work,

ive already tried :smileysad:

 

Please use plain text.
Developer
Developer
oGG
Posts: 55
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel

Re: how to replace this "\" string on json webservice?

You are make something wrong, post new code.
My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! :smileyhappy:
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! :smileyhappy:
Please use plain text.
Developer
simon_hain
Posts: 15,862
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: how to replace this "\" string on json webservice?

check the contents of the json after you parsed it.
it looks to me like a misunderstanding regarding the encoding.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
Benecore
Posts: 385
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: T-Mobile

Re: how to replace this "\" string on json webservice?

Change your simpleSearchFinished as follows

....simpleSearchFinished()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    if (!reply->error()){
        QByteArray response = reply->readAll();
        response.replace("\\", ""); 
        if (response.startsWith("\"")){
            response.remove(0, 1);
        }
        if (response.endsWith("\"")){
            response.remove(response.length()-1, 1);
        }
        emit simpleSearchDone(response);
    }else{
        const int httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        qDebug() << "ErrorCode" << httpCode << endl << \
                "ErrorString" << reply->errorString();
        emit error(httpCode, reply->errorString());
    }
    reply->deleteLater();
    manager->deleteLater();
}

and in QML

function simpleSearch(response){
    indicator.stop()
    model.clear()
    var json = JSON.parse(response) 
    if (json){ // is VALID/PARSED
        model.append(json.search_result)
    }
}

As I wrote you, this is not a good workaround. The best is send VALID JSON string right from your server. But it works

 

:Punk:



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Please use plain text.