0

I have an Android Application and a Server with Node.js that uses Restify.js and MongoJS to store data from and to my android application.

For now, I am using JSONArray from Android (but in my case, it's json-simple lib based on original lib) and I have a JSONArray containing JSONObjects (from json-simple lib as well).

In my case, a JSONObject is as following :

{
    "PLACE_NAME":"Paris",
    "COLOR_ID":"2131099684",
    "LIST_DATES":["2014-05-23","2014-05-22","2014-05-21"]
}

My point here, I have many many JSONObjects that obviously respect the same architecture : a place name, a color ID and list of date(s).

Afterwards, I am storing this JSONObjects in my JSONArray. Simple like that, in a loop, for as many JSONOBject I have :

myJsonArray.add(myJsonObj);

Hence, the content of my JSONArray is as following :

[
    {
        "PLACE_NAME":"Paris",
        "COLOR_ID":"2131099684",
        "LIST_DATES":["2014-05-23","2014-05-22","2014-05-21"]
    },

    {
        "PLACE_NAME":"Milan",
        "COLOR_ID":"2131099667",
        "LIST_DATES":["2014-05-14","2014-05-16","2014-05-15"]
    }
    // ... and it goes on and on
]

So far that data architecture worked very well because I can store in a file and thanks to JSONArray from json-simple lib, I can use a built-in parser that can easily parse the file.

THerefore, when I want to retrieve all the JSONObjects from the JSONArray stored in the file, it is a s simple as that :

final FileReader fr = new FileReader(homeActivitySavesPath.getAbsolutePath());
        final JSONParser parser = new JSONParser();
        this.jsonArray = (JSONArray) parser.parse(fr);
        if (this.jsonArray.size() >= 1) {
            for (int i = 0; i < this.jsonArray.size(); i++) {
                final JSONObject jsonObject = (JSONObject)this.jsonArray.get(i);
                // ... doing some logical code to restore data
                }
 }

As I am completely new in JavaScript I am having a hard time to parse this kind of JSONArray.

To start to understand how to parse, I print the content of the POST request I am sending via an HTTP connection from the Android Application : '

console.log("content of request params -> %s", request.params);

And I get that :

content of request params -> [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Clearly a

[object Object]

is the aforementioned JSONObject right ?

Sor how would you built a loop in JavaScript capable for looping through this kind of JSONArray ?

I would like to store each JSONObject separatly in my MongoDB collection.

Thanks for any help !

Mackovich
  • 3,319
  • 6
  • 35
  • 73
  • The console is simply telling you that it is trying to output an object (which is likely correct given that JSON stands for JavaScript Object Notation. Check here http://stackoverflow.com/questions/1078118/how-do-i-iterate-over-a-json-structure to see how to iterate over JSON data. – armadadrive May 19 '14 at 12:45
  • @armadadrive thank you for your reply. I looked into the link your gave but I am having trouble to iterate through the request paramaters. It would seem that I get an `500 internal error` when trying to access to a single paramater. If I try like this : `request.params[1]` it doesn't work. If I try through a `foreach` : `for (var param in request.params)` the console prints a number, probably the index... this is frustating ... – Mackovich May 19 '14 at 13:08

3 Answers3

0

Yeah you are getting object, you need to read the attributes of the object like this

jsonArray.PLACE_NAME
jsonArray.COLOR_ID

then it will give you the value.

Try this, hope this will help you with your problem.

ajitksharma
  • 4,523
  • 2
  • 21
  • 40
0

Try using jQuery, and as soon as you get the string response, you can parse it with a biult in function that looks like:

function (response){
 var theJsonParsed = $.parseJSON(response);
 var myFirstObject = theJsonParsed[0];
 var placeName = myFirstObject['PLACE_NAME'];
 console.log(placeName); // This should output "Paris"
}

Of course since theJsonParsed it's an array, you can iterate it to get each object.

Hope it helps.

EDIT

Actually, if you place a breakpoint in the js file when you run it in Chrome, you can see exactly how response is coming (should be a String) and how theJsonParsed is built (should be a Object Array).

unmultimedio
  • 1,224
  • 2
  • 13
  • 39
0

Thank you all for your answers, it helped me find the solution that worked for.

The problem was in fact not that difficult, it's just that I had trouble finding a way to iterate through the request parameters.

So this is the source code and it works very well:

server.post({path : TPATH, version : '0.0.1'}, saveNewTravel);


function saveNewTravel(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin','*');
    var JSONArray = req.params;
    for (var it = 0; it < req.params.length; it++) {
        var JSONObject = req.params[it];
        var travel = {};
        travel.LIST_DATES = JSONObject.LIST_DATES;
        travel.PLACE_NAME = JSONObject.PLACE_NAME;
        travel.COLOR_ID = JSONObject.COLOR_ID;
        travels.save(travel, function(error, success) {
            if (success) {
                res.send(201, success);
                return next();
            } else
                return next(error);
        });
    }
}

Using Restify.jsand MongoJS it's so simple and so good ^^

Thank you all !!

PS : A side-question, if I may : in which case defininf a version of my server.post is helpful and/or needed ? That guide helped me set up my servers : https://www.openshift.com/blogs/day-27-restify-build-correct-rest-web-services-in-nodejs

Mackovich
  • 3,319
  • 6
  • 35
  • 73