rsanchez / json

Output ExpressionEngine data in JSON format.
http://devot-ee.com/add-ons/json/
101 stars 234 forks source link

Emplode #52

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi, I created a custom field type uses the text field and explodes the text on new lines, trims, then json_encodes the result so the output looks like:

["test","testing","tester","testy","testtesttest"]

But when I added it into your json plugin it's outputting as:

json_array: "["test","testing","tester","testy","testtesttest"]",

Is there an easy way to have the output not enclosed in quotes?

Here's the custom function I added:

  protected function entries_j_array($entry_id, $field, $field_data)
  {
    $data =  ee()->functions->encode_ee_tags($field_data);
    $my_array = explode("\n", trim($data));

    foreach ($my_array as $k => $v)
    {
        $my_array[$k] = trim($v);
    }
  return json_encode($my_array);
  }
rsanchez commented 8 years ago

It's getting double encoded. I think you should just return the array in your function, and the plugin will handle json encoding it.

ghost commented 8 years ago

Yep - Fixed it:

protected function entries_j_array($entry_id, $field, $field_data)
{
  $my_array = explode("\n", trim($field_data));

      foreach ($my_array as $k => $v)
      {
         $my_array[$k] = trim($v);
      }
    return $my_array;
}

P.S. Thank you for the awesome add on!