JSON object to string

Hey guys,
I need to convert a JSON object to string using java program.
And then i will receive that string in a javascript code.
So please tell me how to convert JSON object to string in java
And then how to convert received object which is string back to JSON object.

PS:I am beginner in javascript coding and I have already searched on google.

Replies

  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    Try this out:
    // implement JSON.stringify serialization
    JSON.stringify = JSON.stringify || function (obj) {
        var t = typeof (obj);
        if (t != "object" || obj === null) {
            // simple data type
            if (t == "string") obj = '"'+obj+'"';
            return String(obj);
        }
        else {
            // recurse array or object
            var n, v, json = [], arr = (obj && obj.constructor == Array);
            for (n in obj) {
                v = obj[n]; t = typeof(v);
                if (t == "string") v = '"'+v+'"';
                else if (t == "object" && v !== null) v = JSON.stringify(v);
                json.push((arr ? "" : '"' + n + '":') + String(v));
            }
            return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
        }
    };
     
    var tmp = {one: 1, two: "2"};
    JSON.stringify(tmp); // '{"one":1,"two":"2"}'

You are reading an archived discussion.

Related Posts

i have taken the dd for bitsat,after that a printout.... now where should i post the printout and along with what it should be posted? and where to paste our...
https://www.instructables.com/id/30lb-Fighting-Robot/?ALLSTEPS
Hi all 😀 I need more info regarding ISRO 2012 exams. I am from CSE in BE Final year. When will be the exam ? When dates will be out...
CEans, I was just going through the records and found that we've an opportunity to break our own posting record! The maximum number of posts made in a day currently...
Greetings CEians, So, I am currently involved in building a prototype security system for houses using Linux Ubuntu. Therefore, if anybody is familiar with Ubuntu are welcomed to provide me...