PHP MVC-将JSON转换为模型数据 [英] PHP MVC - Convert JSON to Model data

查看:62
本文介绍了PHP MVC-将JSON转换为模型数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,从本质上讲,我发现自己编写了一堆样板代码,这些代码从JSON编码的字符串中获取信息,并将这些数据放入MVC Web应用程序中使用的模型中.有接受的方法吗?每个模型都应该有一个关联数组来建模对象转换器吗?我应该为此编写一个实用程序类吗?基本上,我只是想从控制器中删除该代码以精简它们,而我是PHP MVC的新手.我正在使用Kohana 3.2(如果与该问题无关).

So essentially I find myself writing a bunch of boiler plate code that takes info from a JSON encoded string and puts that data into the models used in my MVC web app. Is there an accepted method of doing this? Should every model have an associative array to model object converter? Should there be a utility class I write to do this? Basically, I am just trying to remove that code from my controllers to slim them down and I am new to PHP MVC. I am using Kohana 3.2 if that is of any relevance to the question.

我被要求澄清.我收到以JSON编码的字符串格式的数据(来自我自己写的Web服务-Java/w Jersey).因此,从本质上讲,我的Web应用程序中的模型不是从数据库中获取信息,而是从Web服务中获取信息.由于Web服务以JSON格式返回所有内容,因此我发现自己正在编写处理该问题的代码.另一方面,我可以告诉GSON谷歌代码将JSON转换为特定的Java对象.在PHP中似乎没有一种统一的方法可以做到这一点.我不是在谈论stdClass对象,而是模型.

I was asked to clarify. I receive data in string format that is JSON encoded (from a web service of my own writing - Java /w Jersey). So essentially, the models in my web app are not pulling their information from a database, but rather from a web service. Since the web service returns everything in JSON format, I find myself writing code that deals with that issue. The other way around, I can tell the GSON google code to convert JSON to a particular Java object. There does not seem to be a one liner way to do this in PHP. I am not talking about the stdClass object, but a model.

推荐答案

Kohana的模型(ORM calss)仅适用于数据库记录.如果您想使用Kohana,则必须基于ORM模块编写新模块.该模块实际上可以对模型执行相同的操作(加载,保存),但是可以与您一起输入数据.

Kohana's models (ORM calss) only works with databse records. If you'd like to use Kohana you'll have to write new module based in ORM module. This module can have the actually do same things with model (load, save) but it's gona work with you input data.

这篇关于PHP MVC-将JSON转换为模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆