-2

I have response from database:

{"status":"success","message":"Data selected from database","data":[{"id":1171,"sku":0,"word_one":"one word","description":"","word_two":"two word","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"someone\"},{\"text\":\"sometwo\"}]","UserID":188},
...

{"id":1170,"sku":0,"word_one":"something","description":"","word_two":"some two","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"ever\"},{\"text\":\"never\"}]","UserID":188}]}

Before I make post: angular.toJson($scope.category);

How I can show category something like this

{{category}} = someone, sometwo ?

Because actually I have string :

[{"text":"someone"},{"text":"sometwo"}]
[{"text":"ever"},{"text":"never"}]

...

Cœur
  • 37,241
  • 25
  • 195
  • 267
andrzej
  • 495
  • 1
  • 7
  • 18

2 Answers2

0

You can use the below parsing to achieve what you want. Suppose json is the variable received from database.

var json = {"status":"success","message":"Data selected from database","data":[{"id":1171,"sku":0,"word_one":"one word","description":"","word_two":"two word","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"someone\"},{\"text\":\"sometwo\"}]","UserID":188}]};
var data = json.data[0].category;
var jsonArray = JSON.parse(data);
var category = "";
jsonArray.map(function(obj, i ) {
    if(i != 0) {
      category += ",";
    }
    category += obj.text; 
});
console.log(category)

At the end , You attach category to $scope.category.

Sasank Sunkavalli
  • 3,864
  • 5
  • 31
  • 55
  • I can't get acces to "category" because it is string received from datebase. Maybe I must convert "category" again to array? – andrzej Sep 22 '16 at 20:00
  • SyntaxError: Unexpected token u in JSON at position 0 Maybe because I can't acces to category in this way: var data = json.category; var json in this case looks like: [Object, Object, Object, Object, Object, Object, Object, Object, Object] 0:Object 1:Object 2:Object 3:Object 4:Object 5:Object 6:Object 7:Object 8:Object length:9 __proto__:Array[0] – andrzej Sep 23 '16 at 07:07
  • How does ur JSON object look like ? can u post it here – Sasank Sunkavalli Sep 23 '16 at 07:10
  • [Object, Object, Object] 0:Object 1:Object 2:Object $$hashKey: "object:82" UserID: 188 category: "[{"text":"sometext"},{"text":"someword"}]" description: "" id: 1170 image: "" lang_one: "en" lang_two: "en" mrp: 0 sku: 0 status: "Active" word_one: "one some" word_two: "two some" __proto__: Object length: 9 __proto__: Array[0] – andrzej Sep 23 '16 at 07:19
  • Unexpected token u in JSON at position 0 . This issue comes if the Json what you have is not a proper json . Please check whether u r getting a proper json from backend – Sasank Sunkavalli Sep 23 '16 at 07:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124010/discussion-between-sasank-sunkavalli-and-andrzej). – Sasank Sunkavalli Sep 23 '16 at 07:34
  • When I send data to mysql from input I do this: product.category = angular.toJson($scope.categorys); In php side I have: $app->get('/products', function() { global $db; $rows = $db->select("products","id,sku,word_one,description,word_two,mrp,lang_one,image,lang_two,status,category,UserID",array('UserID' => $_SESSION['uid'])); echoResponse(200, $rows); }); – andrzej Sep 23 '16 at 08:31
  • Thanks! You helped me! – andrzej Sep 23 '16 at 10:11
0

This work:

        var parsed = JSON.parse($scope.c.category);
        var arr = [];
        for(var x in parsed){
          arr.push(parsed[x]);
        }
        $scope.c.category = arr;
andrzej
  • 495
  • 1
  • 7
  • 18