簡體   English   中英

將兩個json數組合並為一個

[英]Merge two json array into one

嘗試將兩個 JSON 響應合並為一個,但問題是數據顯示在兩個數組中,我希望它在一個數組中。 我如何在 Lumen/Laravel 中實現它

嘗試包含兩個數組或響應

 public function index(Request $request)
    {
        $post = Post::orderBy('id', 'DESC')->get();
        $post_images = PostImage::orderBy('id', 'DESC')->get();
        return $this->successResponse($posts.$post_image );        
    }

預期的:-

{
    "post": {
        "id": 14,
        "post_id": 798965728,
        "user_id": 1,
        "location": "first",
        "title": "un8852",
        "cooked_time": "1554329910",
        "dispose_time": "1554373110",
        "food_type": "nv",
        "description": "asdfg",
        "serve_quantity": 23,
        "lat": 19.08,
        "lon": 73,    
        "id": 10,
        "post_id": 798965728,
        "image_name1": null,
        "image_name2": null,
        "image_name3": null,
        "image_name4": null,
        "image_name5": null,
    },
    "st": "1",
    "msg": "success"
}

得到了:-

{
   "post":"{\"id\":14,\"post_id\":798965728,\"user_id\":1,\"location\":\"first\",\"title\":\"un8852\",\"cooked_time\":\"1554329910\",\"dispose_time\":\"1554373110\",\"food_type\":\"nv\",\"description\":\"asdfg\",\"serve_quantity\":23,\"lat\":19.08,\"lon\":73,\"created_at\":\"2019-04-04 10:20:18\",\"updated_at\":\"2019-04-04 10:20:18\"}{\"id\":10,\"post_id\":798965728,\"image_name1\":null,\"image_name2\":null,\"image_name3\":null,\"image_name4\":null,\"image_name5\":null,\"created_at\":\"2019-04-04 10:20:18\",\"updated_at\":\"2019-04-04 10:20:18\"}",
   "st":"1",
   "msg":"success"
}

那里有一些缺失的部分,但我想我明白發生了什么。

根據您得到的結果,此代碼中的$posts$post_image看起來是雄辯的模型。

return $this->successResponse($posts.$post_image ); 

當您連接它們時,它們的__toString()方法將它們轉換為字符串,這是使用toJson()方法完成的。 所以基本上你有兩個 JSON 對象粘在一起,這不是有效的 JSON,然后successResponse()方法再次對它們進行編碼。

要合並它們,您可以將它們轉換為數組,合並它們,然后將結果傳遞給successResponse()

$merged = array_merge($posts->toArray(), $post_image->toArray());
return $this->successResponse($merged);

但是,您想要的結果是不可能的。 “post”對象有兩個不同的“id”值。 你只能得到一個。 如果你使用

$merged = array_merge($posts->toArray(), $post_image->toArray());

然后第二個對象的 id 值將替換第一個。 如果要保留第一個 id 值,則需要使用 union 而不是array_merge

$merged = $a->toArray() + $b->toArray(); 

我認為您不能將 2 個 JSON 對象連接為字符串。

正確的方法是:

  1. 獲取 Post 和 PostImage 對象

    $post = Post::orderBy('id', 'DESC')->get();

    $post_images = PostImage::orderBy('id', 'DESC')->get();

  2. 序列化 Post 對象https://laravel.com/docs/5.8/eloquent-serialization

  3. 使用下面描述的方法將 PostImage 對象的每個字段 (image_name1 .. image_name5) 添加到 JSON 如何在 PHP 中添加 JSON 中的屬性?

  4. 返回 JSON

更新:

Post::orderBy('id','DESC')->get()-> first() 返回一個對象。

Post::orderBy('id','DESC')->get() 返回一個對象集合,它需要一種不同的方法。

嘗試這個:

$post = Post::orderBy('id', 'DESC')->get();

$post->map(function ($item, $key) {
    $post_images = PostImage::where('post_id', $item->id)->get()->first();
    $item->setAttribute('image_name1',$post_images->image_name1);
    $item->setAttribute('image_name2',$post_images->image_name2);
    $item->setAttribute('image_name3',$post_images->image_name3);
    $item->setAttribute('image_name4',$post_images->image_name4);
    $item->setAttribute('image_name5',$post_images->image_name5);
    return $item;
});

return $this->successResponse($posts);

您絕對可以連接兩個 JSON 數組,您必須解析對象並將它們連接起來並重新字符串化。

這個問題也可以在這里回答。 https://stackoverflow.com/a/10384890/1684254

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM