0

How can i split and parse particular json data using angularjs.

My json is

 [{"id":"1", "stud":" name=Alex, roll_no=12 | class=12,sec=1" , "tech":"Sam"},
  {"id":"2", "stud":" name=john, roll_no=13 | class=12,sec=2" , "tech":"Sandy"},
  {"id":"3", "stud":" name=Cate, roll_no=14 | class=12,sec=1" , "tech":"Sandy"},
 ]

I want to parse this json formated data like if ID =1 .

Name= Alex.
Roll No=12.
Class=12.
Section=1.
Teacher=Sam.

2
  • want to fetch data, i know about ng-if and ng-repeat, here i am unable to split and indexof using angular. Commented Mar 2, 2016 at 12:52
  • use angular foreach loop Commented Mar 2, 2016 at 12:55

2 Answers 2

0

Iterate through the collection with reduce. On every Object in the collection parse the stud attribute with Regular Expressions. Create the new string and add it to the new collection.

// Pseudocode
collection.reduce((memo, item) => {
    const values = item.stud.match(" name=Alex, roll_no=12 | class=12,sec=1".match(/^ name=(\w*), roll_no=(\d{2}).../))
    // Add it to memo in a format you like
    return memo
})
Sign up to request clarification or add additional context in comments.

Comments

0

I wrote you some functions...

function getRowFromJson(json, id){
   for(var i=0; i<json.length; i++) 
     if('undefined' == json[i].id) continue;
     else if(json[i].id==id) return json[i];
   return false;
}

function parseStudFromRow(row){
  var stud = {};
  var chunks = row.stud.split("|");
  for(var i=0; i<chunks.length; i++){
    var morechunks = chunks[i].split(",");
    for(var n=0; n<morechunks.length; n++){
      var chunkage = morechunks[n].split("=");
      stud[chunkage[0].trim()] = chunkage[1].trim();
    }
  }
  return stud;
}

You use it like this..

var row = getRowFromJson(json, 1);
var stud = parseStudFromRow(row);
console.log(stud);

Check out the example.. https://jsfiddle.net/o0v6nyzu/2/

2 Comments

i want same thing but how can use in html tag like, i am showing data using ng-repeat,,,,, {{s.stud}} , {{s.teach}}..... i need to filter overhere in {{s.stud}}. Please tell how can i use ur sollution overhere inside {{s.stud}}.
i have no idea, sorry, not familiar with angular. maybe someone smarter than me can tell you how to create what you want with the object provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.