我在将Eloquent数据作为JSON移动到Laravel模板时遇到了麻烦。有问题的数据是用户输入的文本,其中包含换行符,各种特殊字符,引号,连字符,MS Word xml垃圾标签,您可以将其命名。
当我将其打印到模板
时<script>App.data = JSON.parse('{{data}}')</script>
我得到各种JSON解析错误。通常来自换行符,也来自特殊字符等。我现在正在通过丑陋的黑客来摆脱它们,比如
$data->body = str_replace("\n", "\\n", addslashes($data->body))
但必须有一个更优雅的解决方案。使用常规的json_encode()会在JSON中留下换行符等错误。
我正在使用Laravel 5.3和TwigBridge,所以我使用的是Twig模板而不是Blade。
答案 0 :(得分:1)
将对象传递给模板,并使用json_encode
过滤器让twig执行json编码:
{{ data|json_encode() }}
答案 1 :(得分:0)
不要使用json_encode,只需将PHP对象直接传递给您的视图即可。然后做:
List<Flower> flowerList;
ProgressBar progressBar;
List<GetData> task;
public static final String PHOTO_BASE_URL="http://services.hanselandpetal.com/photos/";
private static final int REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_list);
// textView=(TextView)findViewById(R.id.data);
// textView.setMovementMethod(new ScrollingMovementMethod());
progressBar=(ProgressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.INVISIBLE);
task=new ArrayList<>();
if(isOnline())
{
requestData("http://services.hanselandpetal.com/feeds/flowers.json");
}
else
{
Toast.makeText(DisplayList.this, "Network is not Available", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
// if (id == R.id.task1)
// {
//
// }
return super.onOptionsItemSelected(item);
}
private void requestData(String url) {
GetData getData=new GetData();
getData.execute(url);
}
public void update()
{
//Use FlowerAdapter to display data
FlowerAdapter adapter = new FlowerAdapter(this, R.layout.item_flower, flowerList);
setListAdapter(adapter);
}
protected boolean isOnline()
{
ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null && networkInfo.isConnectedOrConnecting())
{
return true;
}
else
{
return false;
}
}
private class GetData extends AsyncTask<String,String,List<Flower>>
{
@Override
protected void onPreExecute()
{
// update("Task Started");
if(task.size()==0) {
progressBar.setVisibility(View.VISIBLE);
}
task.add(this);
}
@Override
protected List<Flower> doInBackground(String... params) {
String content="Internet is to slow";
content=HttpManager1.getDataByHttpUrlConnection(params[0]);
flowerList= FlowerJSONParser.parseFeed(content);
// for(Flower flower:flowerList)
// {
// try
// {
// String imageUrl=PHOTO_BASE_URL+flower.getPhoto();
// InputStream inputStream=(InputStream)new URL(imageUrl).getContent();
// Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
// flower.setBitmap(bitmap);
// inputStream.close();
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
//
// }
return flowerList;
}
@Override
public void onPostExecute(List<Flower> result)
{
update();
task.remove(this);
if(task.size()==0) {
progressBar.setVisibility(View.INVISIBLE);
}
}
@Override
protected void onProgressUpdate(String... values) {
//update(values[0]);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Flower flower=flowerList.get(position);
Intent intent=new Intent(this,DetailActivity.class);
intent.putExtra("flowerName",flower.getName());
intent.putExtra("imageBitmap",flower.getBitmap());
intent.putExtra("instruction",flower.getInstructions());
startActivityForResult(intent, REQUEST_CODE);
}
}
</pre>
答案 2 :(得分:0)
我最终跳过整个json编码/解析,现在我只是将对象直接打印到模板。
所以而不是
//Controller
$data = json_encode($data);
//View
var data = JSON.parse('{{data}}');
//Prints out
// var data = JSON.parse('{\"id\":44,\"code\":\"2017-E0044\...}');
我只是做
//View
var data = {{data}};
//Prints out
//var data = {"id":44,"code":"2017-E0044"...};