-3

I have a json like this :

{
   "Project 
      [id=1, dateDebut=2017-01-13, dateFin=2017-01-18, description=qsd, sponsor=qsd ]"
    :
 [
  {"id":1,"title":"qsd ","description":"qsdqsd","dateFin":"2017-01-26"},
  {"id":2,"title":"sss ","description":"sss","dateFin":"2017-01-26"}
 ]
}

originated from : return new ObjectMapper.write(Map<Project,List<Task>> projectTasks = new LinkedMultiValueMap<>()) ;

EDIT : this is the real response :

{"Project [id=1, name=qsdsqd, type=null, done=false, dateDebut=2017-01-13, dateFin=2017-01-18, description=qsd, sponsor=qsd, client=qsd, showable=true]":
[{"id":1,"title":"qsd ","description":"qsdqsd","dateFin":"2017-01-26","dateDebut":"2017-01-14","period":null,"done":false,"status":"Actif","priority":"Normal"},
{"id":2,"title":"task 2 ","description":"qsdqsd","dateFin":"2017-01-26","dateDebut":"2017-01-14","period":null,"done":false,"status":"Actif","priority":"Normal"}]}

How can I read the list of tasks in the client side ?

OddDev
  • 1,521
  • 7
  • 23
  • 45

2 Answers2

0

First of all, your JSON is not valid. Are you sure that is a line break between the word Project and [id...]. A valid JSON would be:

{
   "Project [id=1, dateDebut=2017-01-13, dateFin=2017-01-18, description=qsd, sponsor=qsd, ]":
 [
  {"id":1,"title":"qsd ","description":"qsdqsd","dateFin":"2017-01-26"},
  {"id":2,"title":"sss ","description":"sss","dateFin":"2017-01-26"}
 ]
}

You can have object key names like that. But i'ts not very friendly to retrieve data.

If you cannot change your data schema (or just don't want), you can iterate over the Object with the

Object.keys(obj).forEach ( (key) => {
   console.log('key: ' + key);
   console.log('value: ' + obj[key]);

   /* you can iterate over your value (tasks) here */
   obj[key].forEach( (task) => {
      console.log('task1: ', task);
   });

});  //where obj is your json

Or you can access the first object property with:

obj[Object.keys(obj)[0]]; //where obj is your json

EDIT As pointed by @André Dion, forEachis best suited to iteration, not map. And we're assuming your response is already parsed from the server (by yourself or by a lib like jquery). If not, you should do a JSON.parse(response); to retrieve the object.

mrlew
  • 7,078
  • 3
  • 25
  • 28
  • 1
    Don't use `Array#map` to iterate, that's `Array#forEach`'s job. This also assumes an Object, not JSON. You would need to call `JSON.parse` on the response first. – André Dion Jan 12 '17 at 17:42
  • @AndréDion [Object.keys](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) return an Array. Yes, I'm assuming it's an already parsed object. – mrlew Jan 12 '17 at 17:44
  • I'm aware of how `Object.keys` works. My points still stand: don't iterate with `map` and if `obj` represents the server response, you'd need `var obj = JSON.parse(response)` first. – André Dion Jan 12 '17 at 17:47
  • I get it. You're right. I am very used with map. Will edit. – mrlew Jan 12 '17 at 17:50
0

You may try this: Assume above response in stored in var response.

for(var project in response) { // this will get every project
    for(var i=0; i<project.length; i++) { // this will iterate over the array for each project which are your tasks.
         console.log("task" + project[i]);
         console.log(project[i]["id"]); // access each tasks id, similar for other attributes
    }
}
Anurag Sinha
  • 1,014
  • 10
  • 17