2017-01-18 2 views
0

APIから返されたユーザーのJSONをフォーマットする最適な方法を理解しようとしています。プレミアム機能の有無にかかわらずユーザーJSONをフォーマットする

user { 
    name: 'john', 
    email: '[email protected]', 
    signed_up: '12-12-2012', 
} 

JSONにユーザーのプレミアムプラン機能を追加するにはどうすればよいですか?ユーザーはFree、またはProまたはBusinessのいずれかになります。問題は、ProBusinessの両方に、プランに関する詳細情報を含むオブジェクトが含まれ、Freeプランには何も含まれていないということです。

Freeの場合にはプランの文字列を返し、支払われる場合はオブジェクトを返してもよろしいですか?または、異なる戻り値の型を使用しないでください。

答えて

1

無料ユーザー:

response: { 
    user: { 
    name: 'john', 
    email: '[email protected]', 
    signed_up: '12-12-2012', 
    plan: { 
     name: 'Free', 
     billing_period: null 
     price: null 
    } 
    }, 
} 

プレミアムユーザー:

response: { 
    user: { 
    name: 'john', 
    email: '[email protected]', 
    signed_up: '12-12-2012', 
    plan: { 
     name: 'Premium', 
     billing_period: 12 
     price: 299 
    } 
    }, 
} 
1

以下のように応答をフォーマットすることができます。プレミアムがProまたはBusinessの場合、furtherInfoオブジェクトはさらに情報を持ちます。それ以外の場合は、Freeの場合は、responseに詳細情報を含めるべきではありません。私は次の形式と一緒に行くことになった

response: { 
    user: { 
    name: 'john', 
    email: '[email protected]', 
    signed_up: '12-12-2012', 
    premium: 'Pro' 
    }, 
    furtherInfo: { 
    attribute1: 'value1', 
    .... 
    attributeN: 'valueN' 
    } 
} 

OR

response: { 
    user: { 
    name: 'john', 
    email: '[email protected]', 
    signed_up: '12-12-2012', 
    premium: 'Free' 
    } 
} 
関連する問題